Which class I should override in order to redirect a customer to a specific page after login?
I have tried to set Redirect Customer to Account Dashboard after Logging in
in the store configuration but it is not working.
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityWhich class I should override in order to redirect a customer to a specific page after login?
I have tried to set Redirect Customer to Account Dashboard after Logging in
in the store configuration but it is not working.
A plugin is a better solution in this case because your extended class might need to be updated when Magento 2 updates.
Here is a solution using a after-plugin on LoginPost->execute() as suggested by Xenocide8998.
/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\LoginPost">
<plugin name="vendor_module_loginpostplugin" type="\Vendor\Module\Plugin\LoginPostPlugin" sortOrder="1" />
</type>
</config>
/Vendor/Module/Plugin/LoginPostPlugin.php
:
<?php
/**
*
*/
namespace Vendor\Module\Plugin;
/**
*
*/
class LoginPostPlugin
{
/**
* Change redirect after login to home instead of dashboard.
*
* @param \Magento\Customer\Controller\Account\LoginPost $subject
* @param \Magento\Framework\Controller\Result\Redirect $result
*/
public function afterExecute(
\Magento\Customer\Controller\Account\LoginPost $subject,
$result)
{
$result->setPath('/'); // Change this to what you want
return $result;
}
}
I resolved it by overriding the LoginPost class
etc/di.xml
<preference for="Magento\Customer\Controller\Account\LoginPost" type="Vendor\Module\Controller\Account\LoginPost" />
Vendor/Module/Controller/Account/LoginPost.php
<?php
namespace Vendor\Module\Controller\Account;
use Magento\Customer\Model\Account\Redirect as AccountRedirect;
use Magento\Framework\App\Action\Context;
use Magento\Customer\Model\Session;
use Magento\Customer\Api\AccountManagementInterface;
use Magento\Customer\Model\Url as CustomerUrl;
use Magento\Framework\Exception\EmailNotConfirmedException;
use Magento\Framework\Exception\AuthenticationException;
use Magento\Framework\Data\Form\FormKey\Validator;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class LoginPost extends \Magento\Customer\Controller\Account\LoginPost {
public function execute() {
if ($this->session->isLoggedIn() || !$this->formKeyValidator->validate($this->getRequest())) {
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('home');
return $resultRedirect;
}
if ($this->getRequest()->isPost()) {
$login = $this->getRequest()->getPost('login');
if (!empty($login['username']) && !empty($login['password'])) {
try {
$customer = $this->customerAccountManagement->authenticate($login['username'], $login['password']);
$this->session->setCustomerDataAsLoggedIn($customer);
$this->session->regenerateId();
} catch (EmailNotConfirmedException $e) {
$value = $this->customerUrl->getEmailConfirmationUrl($login['username']);
$message = __(
'This account is not confirmed.' .
' <a href="%1">Click here</a> to resend confirmation email.', $value
);
$this->messageManager->addError($message);
$this->session->setUsername($login['username']);
} catch (AuthenticationException $e) {
$message = __('Invalid login or password.');
$this->messageManager->addError($message);
$this->session->setUsername($login['username']);
} catch (\Exception $e) {
$this->messageManager->addError(__('Invalid login or password.'));
}
} else {
$this->messageManager->addError(__('A login and a password are required.'));
}
}
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('home');
return $resultRedirect;
}
}
afterExecute()
would be a cleaner option
Nov 4, 2016 at 4:31
The simplest solution that i found
If the custom URL is sales/order/history
Vendor/Extension/etc/frontend/di.xml
<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\LoginPost">
<plugin name="redirect_custom_url" type="VendorName\Extension\Plugin\RedirectCustomUrl" sortOrder="1"/>
</type>
</config>
Vendor/Extension/Plugin/RedirectCustomUrl.php
<?php
namespace Vendor\Extension\Plugin;
class RedirectCustomUrl
{
public function afterExecute(
\Magento\Customer\Controller\Account\LoginPost $subject,
$result)
{
$customUrl = 'sales/order/history';
$result->setPath($customUrl);
return $result;
}
}
Vote this answer up if it helps.
That the current local storage caused our issue.
If we enable or disable Redirect Customer to Account Dashboard after Logging in
and Guest Checkout in Configuration, this feature will work well. However, we need to clear your local storage.
We can check the local storage localStorage.getItem('mage-cache-storage')
.
Take a look:
vendor/magento/module-checkout/view/frontend/web/js/sidebar.js
var cart = customerData.get('cart'),
customer = customerData.get('customer');
if (!customer().firstname && cart().isGuestCheckoutAllowed === false) {
// set URL for redirect on successful login/registration. It's postprocessed on backend.
$.cookie('login_redirect', this.options.url.checkout);
if (this.options.url.isRedirectRequired) {
location.href = this.options.url.loginUrl;
} else {
authenticationPopup.showModal();
}
return false;
}
Magento will set the cookie $.cookie('login_redirect', this.options.url.checkout)
based on customerData
from local storage.
From the controller vendor/magento/module-customer/Controller/Account/LoginPost.php
. It will check the redirect URL from the cookie.
$redirectUrl = $this->accountRedirect->getRedirectCookie();
if (!$this->getScopeConfig()->getValue('customer/startup/redirect_dashboard') && $redirectUrl) {
......
return $resultRedirect;
}
Magento version:
-Magento version 2.1.0
I resolved this by passing referer in custom module controller.
Step1 `
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Customer\Model\Session;
use Magento\Framework\UrlInterface;
class Approve extends \Magento\Framework\App\Action\Action {
/**
* @var \Magento\Framework\View\Result\Page
*/
protected $resultPageFactory;
/**
* $param \Magento\Framework\App\Action\Context $context */
/**
* @param CustomerSession
*/
protected $_customerSession;
protected $_urlInterface;
public function __construct(
Context $context,
PageFactory $resultPageFactory,
Session $customerSession,
UrlInterface $urlInterface
)
{
$this->resultPageFactory = $resultPageFactory;
$this->_customerSession = $customerSession;
$this->_urlInterface = $urlInterface;
parent::__construct($context);
}
public function execute(){
$url = $this->_urlInterface->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]);
// here pass custom url or you can either use current url on which you are currently and want to come back after logged in.
$loginUrl = $this->_urlInterface->getUrl('customer/account/login', array('referer' => base64_encode($url)));
if($this->_customerSession->isLoggedIn()){
return $this->resultPageFactory->create();
}
$this->_redirect($loginUrl);
}
}`
Step2
Go to Admin : Store > Configuration > Customers > Customer Configuration > Login Options > Redirect Customer to Account Dashboard after Logging in > No
I've created a module that allows me to specify the redirectUrl
via querystring when redirecting customers to /customer/account/login
.
For example /customer/account/login?redirectUrl=https://someurl.com or ?redirectUrl=some/path.
I did this by creating 2 plugins:
Magento\Customer\Controller\Account\Login
Just to capture the parameter redirectUrl
and save it to the customer session.
use Magento\Customer\Model\Session;
use Magento\Framework\App\Request\Http;
class CustomerLoginPlugin
{
private Session $session;
private Http $httpRequest;
public function __construct(Session $session, Http $httpRequest)
{
$this->session = $session;
$this->httpRequest = $httpRequest;
}
/**
* @param \Magento\Customer\Controller\Account\Login $subject
* @param $result
*/
public function afterExecute(\Magento\Customer\Controller\Account\Login $subject, $result)
{
if ($redirectUrl = $this->httpRequest->getParam('redirectUrl', false)) {
$this->session->setRedirectUrl($redirectUrl);
}
return $result;
}
}
Magento\Customer\Controller\Account\LoginPost
This will change the response in case the response is a redirect. So, if we have no errors in the loginPost response, I change the redirect url.
use Magento\Customer\Controller\Account\LoginPost;
use Magento\Customer\Model\Session;
class CustomerLoginPostPlugin
{
private Session $session;
public function __construct(Session $session)
{
$this->session = $session;
}
/**
* @param \Magento\Customer\Controller\Account\LoginPost $subject
* @param $result
*/
public function afterExecute(\Magento\Customer\Controller\Account\LoginPost $subject, $result)
{
if ($result instanceof \Magento\Framework\Controller\Result\Redirect && $this->session->getRedirectUrl()) {
$result->setPath($this->session->getRedirectUrl());
}
return $result;
}
}
if (!$this->customerSession->isLoggedIn()) {
$resultRedirect = $this->redirectFactory->create();
$resultRedirect->setPath('customer/account/login', ['redirectUrl'=>'some/pathOrUrl']);
return $resultRedirect;
}
Note that if you want to always redirect to some specific url, you can ignore the first Plugin, and just replace the setPath
parameter to whatever you want (answering the original question).