Custom redirect user to specific page after register Observer in Magento 2 will just redirect after the cms page to the login page

magento.stackexchange https://magento.stackexchange.com/questions/193865

Question

I have another problem with my custom observer module for account registration page redirect.

somehow it will successfully redirect to my custom cms thank you page after registration. but after that one, it will ask me to log in after registration. normally it should be automatically logged in instead of asking me to log in again.

Here's the codes:

app/code/Vendor/Module/etc/frontend/events.xml

<event name="customer_register_success">
    <observer name="registerAfter" instance="Vendor\Module\Observer\RegisterAfter" />
</event> 

app/code/Vendor/Module/Observer/RegisterAfter.php

{
    protected $_responseFactory;

    protected $_redirect;

    protected $_url;

    public function __construct(
        \Magento\Framework\View\Element\BlockFactory $blockFactory,
        \Magento\Framework\App\ResponseFactory $responseFactory,
        \Magento\Framework\UrlInterface $url,
        \Magento\Framework\App\Response\Http $redirect
    ) {
        $this->_responseFactory = $responseFactory;
        $this->_url = $url;
        $this->_redirect = $redirect;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $customRedirectionUrl = $this->_url->getUrl('thank-you-account-creation');
        $this->_responseFactory->create()->setRedirect($customRedirectionUrl)->sendResponse();
        die();           
    }
}
Était-ce utile?

La solution

You does not login because of before execute of $this->session->setCustomerDataAsLoggedIn($customer); code your observer is redirect to your page.

if you will check at Magento\Customer\Controller\Account\CreatePost.php

then you understand than customer_register_success **#L309** event
then login function $this->session->setCustomerDataAsLoggedIn($customer);

So,Solution,

You have two solution.

1.Set Customer in session from observer

protected $session;
public function __construct(
    \Magento\Framework\View\Element\BlockFactory $blockFactory,
    \Magento\Framework\App\ResponseFactory $responseFactory,
    \Magento\Framework\UrlInterface $url,
    \Magento\Framework\App\Response\Http $redirect,
    Magento\Customer\Model\Session $customerSession
) {
    $this->_responseFactory = $responseFactory;
    $this->_url = $url;
    $this->_redirect = $redirect;
    $this->session = $customerSession;
}

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $customer = $observer->getCustomer();
    /* Customer loggin in session */
    $this->session->setCustomerDataAsLoggedIn($customer);

    $customRedirectionUrl = $this->_url->getUrl('thank-you-account-creation');
    $this->_responseFactory->create()->setRedirect($customRedirectionUrl)->sendResponse();
}

Solution2:

Best solution,set Customer session setBeforeAuthUrl to $customRedirectionUrl and remove

$customRedirectionUrl = $this->_url->getUrl('thank-you-account-creation'); $this->_responseFactory->create()->setRedirect($customRedirectionUrl)->sendResponse(); and Must make sure Redirect Customer to Account Dashboard after Logging in to No For solution 2


protected $session;
public function __construct(
    \Magento\Framework\View\Element\BlockFactory $blockFactory,
    \Magento\Framework\App\ResponseFactory $responseFactory,
    \Magento\Framework\UrlInterface $url,
    \Magento\Framework\App\Response\Http $redirect,
    Magento\Customer\Model\Session $customerSession
) {
    $this->_responseFactory = $responseFactory;
    $this->_url = $url;
    $this->_redirect = $redirect;
    $this->session = $customerSession;
}

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $customer = $observer->getCustomer();
    $customRedirectionUrl = $this->_url->getUrl('thank-you-account-creation');
    $this->session>setBeforeAuthUrl($successUrl); 
    return $this;

}
Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange
scroll top