0

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();           
    }
}
2

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;

}
  • Hi @Amit Bera, Thanks, I will try your code. and update to you once it is working. – MazeStricks Sep 20 '17 at 3:02
  • Hi @Amit Bera, Thank you! it's working like charm! – MazeStricks Sep 20 '17 at 3:11

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.