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();
}
}
Best Answer
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 c
ustomer_register_success **#L309**
eventthen login function
$this->session->setCustomerDataAsLoggedIn($customer);
So,Solution,
You have two solution.
1.Set Customer in session from observer
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 sureRedirect Customer to Account Dashboard after Logging in
to No For solution 2