Magento – Redirect to login page after registration in Magento 2

magento2

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?

Best Answer

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;
    }
}
Related Topic