2

I am using Magento 2.1.0 and I am trying to redirect a customer to the login page after successful registration.

I make this "Redirect Customer to Account Dashboard after Logging in" set to no, after that, it redirects to the home page, but customer is also logged in.

How to stop the customer from being logged in after registration, and redirect them to the login page?

7
  • Your store need confirmation after customer registration?
    – Sohel Rana
    Feb 1 '17 at 5:48
  • how to create conformation for registration???
    – sam
    Feb 1 '17 at 5:55
  • 1
    Stores->Customer->Customer Configuration -> Create new account option->Require Emails Confirmation set to yes
    – Sohel Rana
    Feb 1 '17 at 6:12
  • No i don't want any email conformation. i just want after registration , the landing page will be login page for login
    – sam
    Feb 1 '17 at 6:36
  • I override the createPost.php and comment the line "$this->session->setCustomerDataAsLoggedIn($customer);" , after that registration is successful, but customer is not login.
    – sam
    Feb 1 '17 at 6:38
8

Simply add following line into your overwrite class.

$resultRedirect->setUrl($this->url->getUrl('customer/account/login'));

OR

You can do it using plugin as follows

Vendor/Module/etc/frontend/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Customer\Controller\Account\CreatePost">
        <plugin name="sr_Customer_Account_CreatePost" type="Vendor\Module\Plugin\Customer\Account\CreatePost" sortOrder="1"/>
    </type>
</config>

Vendor/Module/Plugin/Customer/Account/CreatePost.php

namespace Vendor\Module\Plugin\Customer\Account;

class CreatePost
{

    /**
     * @var \Magento\Framework\UrlInterface
     */
    protected $url;

    /**
     * @param \Magento\Framework\UrlInterface $url
     */
    public function __construct(
        \Magento\Framework\UrlInterface $url
    )
    {
        $this->url = $url;
    }

    public function afterExecute(
        \Magento\Customer\Controller\Account\CreatePost $subject,
        $resultRedirect
    ) {
        $resultRedirect->setUrl($this->url->getUrl('customer/account/login'));
        return $resultRedirect;
    }
}
6
  • Yes, this plugin works to redirect login page. thank you
    – sam
    Feb 1 '17 at 6:56
  • @sohelRana, Thank you for providing solution. I have created the plugin as you explained, but still it redirecting to My Account Dashboard page after customer Registration. Can you please help me out from this problem. Feb 2 '17 at 13:15
  • @VenuJoginpally you need override the createPost.php and comment the line "$this->session->setCustomerDataAsLoggedIn($customer);" , after that registration is successful, but customer is not login.
    – Sohel Rana
    Feb 2 '17 at 14:33
  • Is it same applicable for login Post action also and other???
    – sam
    Feb 22 '17 at 7:31
  • Try to use same way. It should work.
    – Sohel Rana
    Feb 22 '17 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.