Magento 2 override the cutomer account loginpost controller
I am trying to overriding the below class in my custom module:
vendormagentomodule-customerControllerAccountLoginPost.php
For overriding this I have created the Plugin file as per the below Magento 2 official reference:
http://devdocs.magento.com/guides/v2.0/extension-dev-guide/plugins.html
I have created the app/code/Vendor/MyModule/etc/di.xml file:
<?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="MagentoCustomerControllerAccountLoginPost">
<plugin name="MyCucomtLoginAccountLoginPost" type="VendorMyModulePluginCustomerLoginPost" sortOrder="10" disabled="false"/>
</type>
</config>
Now, I have written the below is aroundExecute method to achieve my customization:
appcodeVendorMyModulePluginCustomer
<?php
namespace VendorMyModulePluginCustomer;
class LoginPost
{
public function aroundExecute(MagentoCustomerControllerAccountLoginPost $subject, Closure $proceed)
{
//echo 'here'; die;
if (isset($login['press_room_page'])) {
$custom_redirect=true;
}
if (isset($login['press_room_page']) && $custom_redirect) {
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('mycustomlogin/index');
return $resultRedirect;
}
}
}
?>
The above code is working fine when I am print something "here" it means the file is override successfully.
Now I am adding my logic in the execute method below is original file of vendor which I have edited for testing purpose to check the logic work or not:
vendormagentomodule-customerControllerAccount
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace MagentoCustomerControllerAccount;
use MagentoCustomerModelAccountRedirect as AccountRedirect;
use MagentoFrameworkAppActionContext;
use MagentoCustomerModelSession;
use MagentoCustomerApiAccountManagementInterface;
use MagentoCustomerModelUrl as CustomerUrl;
use MagentoFrameworkExceptionEmailNotConfirmedException;
use MagentoFrameworkExceptionAuthenticationException;
use MagentoFrameworkDataFormFormKeyValidator;
use MagentoFrameworkExceptionLocalizedException;
use MagentoFrameworkExceptionStateUserLockedException;
use MagentoFrameworkAppConfigScopeConfigInterface;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class LoginPost extends MagentoCustomerControllerAbstractAccount
{
/** @var AccountManagementInterface */
protected $customerAccountManagement;
/** @var Validator */
protected $formKeyValidator;
/**
* @var AccountRedirect
*/
protected $accountRedirect;
/**
* @var Session
*/
protected $session;
/**
* @var ScopeConfigInterface
*/
private $scopeConfig;
/**
* @var MagentoFrameworkStdlibCookieCookieMetadataFactory
*/
private $cookieMetadataFactory;
/**
* @var MagentoFrameworkStdlibCookiePhpCookieManager
*/
private $cookieMetadataManager;
/**
* @param Context $context
* @param Session $customerSession
* @param AccountManagementInterface $customerAccountManagement
* @param CustomerUrl $customerHelperData
* @param Validator $formKeyValidator
* @param AccountRedirect $accountRedirect
*/
public function __construct(
Context $context,
Session $customerSession,
AccountManagementInterface $customerAccountManagement,
CustomerUrl $customerHelperData,
Validator $formKeyValidator,
AccountRedirect $accountRedirect
) {
$this->session = $customerSession;
$this->customerAccountManagement = $customerAccountManagement;
$this->customerUrl = $customerHelperData;
$this->formKeyValidator = $formKeyValidator;
$this->accountRedirect = $accountRedirect;
parent::__construct($context);
}
/**
* Login post action
*
* @return MagentoFrameworkControllerResultRedirect
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function execute()
{
if ($this->session->isLoggedIn() || !$this->formKeyValidator->validate($this->getRequest())) {
/** @var MagentoFrameworkControllerResultRedirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('*/*/');
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();
if ($this->getCookieManager()->getCookie('mage-cache-sessid')) {
$metadata = $this->getCookieMetadataFactory()->createCookieMetadata();
$metadata->setPath('/');
$this->getCookieManager()->deleteCookie('mage-cache-sessid', $metadata);
}
$redirectUrl = $this->accountRedirect->getRedirectCookie();
if (!$this->getScopeConfig()->getValue('customer/startup/redirect_dashboard') && $redirectUrl) {
$this->accountRedirect->clearRedirectCookie();
$resultRedirect = $this->resultRedirectFactory->create();
// URL is checked to be internal in $this->_redirect->success()
$resultRedirect->setUrl($this->_redirect->success($redirectUrl));
return $resultRedirect;
}
} 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 (UserLockedException $e) {
$message = __(
'The account is locked. Please wait and try again or contact %1.',
$this->getScopeConfig()->getValue('contact/email/recipient_email')
);
$this->messageManager->addError($message);
$this->session->setUsername($login['username']);
} catch (AuthenticationException $e) {
if (isset($login['my_custom_page'])) {
$custom_redirect=true;
}
$message = __('Invalid login or password.');
$this->messageManager->addError($message);
$this->session->setUsername($login['username']);
} catch (LocalizedException $e) {
$message = $e->getMessage();
$this->messageManager->addError($message);
$this->session->setUsername($login['username']);
} catch (Exception $e) {
// PA DSS violation: throwing or logging an exception here can disclose customer password
$this->messageManager->addError(
__('An unspecified error occurred. Please contact us for assistance.')
);
}
} else {
$this->messageManager->addError(__('A login and a password are required.'));
}
}
if (isset($login['my_custom_page']) && $custom_redirect) {
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('mycustomlogin/index');
return $resultRedirect;
}
return $this->accountRedirect->getRedirect();
}
}
Please see the below image to better understand what I have added my logic:
Please see below is second image to written logic for redirect user to again if they enter wrong username or password:
Below is the main moto for override the controller:
I have created the custom login page for specific customer group, they will login from the custom design page, so I have created the mycustomlogin.phtml in my module like: appcodeVendorMyModuleviewfrontendtemplatesmycustomlogin.phtml
and passed the hidden input field value in the form for check where user had posted the form. I have get the hidden input value in AuthenticationException $e
to check. if user have posted the form from custom design.
Anyone can suggest me how I can add my logic into the Plugin file?
Thanks!
magento2 customer-account customer-group magento2.1.5 redirection
add a comment |
I am trying to overriding the below class in my custom module:
vendormagentomodule-customerControllerAccountLoginPost.php
For overriding this I have created the Plugin file as per the below Magento 2 official reference:
http://devdocs.magento.com/guides/v2.0/extension-dev-guide/plugins.html
I have created the app/code/Vendor/MyModule/etc/di.xml file:
<?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="MagentoCustomerControllerAccountLoginPost">
<plugin name="MyCucomtLoginAccountLoginPost" type="VendorMyModulePluginCustomerLoginPost" sortOrder="10" disabled="false"/>
</type>
</config>
Now, I have written the below is aroundExecute method to achieve my customization:
appcodeVendorMyModulePluginCustomer
<?php
namespace VendorMyModulePluginCustomer;
class LoginPost
{
public function aroundExecute(MagentoCustomerControllerAccountLoginPost $subject, Closure $proceed)
{
//echo 'here'; die;
if (isset($login['press_room_page'])) {
$custom_redirect=true;
}
if (isset($login['press_room_page']) && $custom_redirect) {
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('mycustomlogin/index');
return $resultRedirect;
}
}
}
?>
The above code is working fine when I am print something "here" it means the file is override successfully.
Now I am adding my logic in the execute method below is original file of vendor which I have edited for testing purpose to check the logic work or not:
vendormagentomodule-customerControllerAccount
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace MagentoCustomerControllerAccount;
use MagentoCustomerModelAccountRedirect as AccountRedirect;
use MagentoFrameworkAppActionContext;
use MagentoCustomerModelSession;
use MagentoCustomerApiAccountManagementInterface;
use MagentoCustomerModelUrl as CustomerUrl;
use MagentoFrameworkExceptionEmailNotConfirmedException;
use MagentoFrameworkExceptionAuthenticationException;
use MagentoFrameworkDataFormFormKeyValidator;
use MagentoFrameworkExceptionLocalizedException;
use MagentoFrameworkExceptionStateUserLockedException;
use MagentoFrameworkAppConfigScopeConfigInterface;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class LoginPost extends MagentoCustomerControllerAbstractAccount
{
/** @var AccountManagementInterface */
protected $customerAccountManagement;
/** @var Validator */
protected $formKeyValidator;
/**
* @var AccountRedirect
*/
protected $accountRedirect;
/**
* @var Session
*/
protected $session;
/**
* @var ScopeConfigInterface
*/
private $scopeConfig;
/**
* @var MagentoFrameworkStdlibCookieCookieMetadataFactory
*/
private $cookieMetadataFactory;
/**
* @var MagentoFrameworkStdlibCookiePhpCookieManager
*/
private $cookieMetadataManager;
/**
* @param Context $context
* @param Session $customerSession
* @param AccountManagementInterface $customerAccountManagement
* @param CustomerUrl $customerHelperData
* @param Validator $formKeyValidator
* @param AccountRedirect $accountRedirect
*/
public function __construct(
Context $context,
Session $customerSession,
AccountManagementInterface $customerAccountManagement,
CustomerUrl $customerHelperData,
Validator $formKeyValidator,
AccountRedirect $accountRedirect
) {
$this->session = $customerSession;
$this->customerAccountManagement = $customerAccountManagement;
$this->customerUrl = $customerHelperData;
$this->formKeyValidator = $formKeyValidator;
$this->accountRedirect = $accountRedirect;
parent::__construct($context);
}
/**
* Login post action
*
* @return MagentoFrameworkControllerResultRedirect
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function execute()
{
if ($this->session->isLoggedIn() || !$this->formKeyValidator->validate($this->getRequest())) {
/** @var MagentoFrameworkControllerResultRedirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('*/*/');
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();
if ($this->getCookieManager()->getCookie('mage-cache-sessid')) {
$metadata = $this->getCookieMetadataFactory()->createCookieMetadata();
$metadata->setPath('/');
$this->getCookieManager()->deleteCookie('mage-cache-sessid', $metadata);
}
$redirectUrl = $this->accountRedirect->getRedirectCookie();
if (!$this->getScopeConfig()->getValue('customer/startup/redirect_dashboard') && $redirectUrl) {
$this->accountRedirect->clearRedirectCookie();
$resultRedirect = $this->resultRedirectFactory->create();
// URL is checked to be internal in $this->_redirect->success()
$resultRedirect->setUrl($this->_redirect->success($redirectUrl));
return $resultRedirect;
}
} 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 (UserLockedException $e) {
$message = __(
'The account is locked. Please wait and try again or contact %1.',
$this->getScopeConfig()->getValue('contact/email/recipient_email')
);
$this->messageManager->addError($message);
$this->session->setUsername($login['username']);
} catch (AuthenticationException $e) {
if (isset($login['my_custom_page'])) {
$custom_redirect=true;
}
$message = __('Invalid login or password.');
$this->messageManager->addError($message);
$this->session->setUsername($login['username']);
} catch (LocalizedException $e) {
$message = $e->getMessage();
$this->messageManager->addError($message);
$this->session->setUsername($login['username']);
} catch (Exception $e) {
// PA DSS violation: throwing or logging an exception here can disclose customer password
$this->messageManager->addError(
__('An unspecified error occurred. Please contact us for assistance.')
);
}
} else {
$this->messageManager->addError(__('A login and a password are required.'));
}
}
if (isset($login['my_custom_page']) && $custom_redirect) {
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('mycustomlogin/index');
return $resultRedirect;
}
return $this->accountRedirect->getRedirect();
}
}
Please see the below image to better understand what I have added my logic:
Please see below is second image to written logic for redirect user to again if they enter wrong username or password:
Below is the main moto for override the controller:
I have created the custom login page for specific customer group, they will login from the custom design page, so I have created the mycustomlogin.phtml in my module like: appcodeVendorMyModuleviewfrontendtemplatesmycustomlogin.phtml
and passed the hidden input field value in the form for check where user had posted the form. I have get the hidden input value in AuthenticationException $e
to check. if user have posted the form from custom design.
Anyone can suggest me how I can add my logic into the Plugin file?
Thanks!
magento2 customer-account customer-group magento2.1.5 redirection
add a comment |
I am trying to overriding the below class in my custom module:
vendormagentomodule-customerControllerAccountLoginPost.php
For overriding this I have created the Plugin file as per the below Magento 2 official reference:
http://devdocs.magento.com/guides/v2.0/extension-dev-guide/plugins.html
I have created the app/code/Vendor/MyModule/etc/di.xml file:
<?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="MagentoCustomerControllerAccountLoginPost">
<plugin name="MyCucomtLoginAccountLoginPost" type="VendorMyModulePluginCustomerLoginPost" sortOrder="10" disabled="false"/>
</type>
</config>
Now, I have written the below is aroundExecute method to achieve my customization:
appcodeVendorMyModulePluginCustomer
<?php
namespace VendorMyModulePluginCustomer;
class LoginPost
{
public function aroundExecute(MagentoCustomerControllerAccountLoginPost $subject, Closure $proceed)
{
//echo 'here'; die;
if (isset($login['press_room_page'])) {
$custom_redirect=true;
}
if (isset($login['press_room_page']) && $custom_redirect) {
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('mycustomlogin/index');
return $resultRedirect;
}
}
}
?>
The above code is working fine when I am print something "here" it means the file is override successfully.
Now I am adding my logic in the execute method below is original file of vendor which I have edited for testing purpose to check the logic work or not:
vendormagentomodule-customerControllerAccount
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace MagentoCustomerControllerAccount;
use MagentoCustomerModelAccountRedirect as AccountRedirect;
use MagentoFrameworkAppActionContext;
use MagentoCustomerModelSession;
use MagentoCustomerApiAccountManagementInterface;
use MagentoCustomerModelUrl as CustomerUrl;
use MagentoFrameworkExceptionEmailNotConfirmedException;
use MagentoFrameworkExceptionAuthenticationException;
use MagentoFrameworkDataFormFormKeyValidator;
use MagentoFrameworkExceptionLocalizedException;
use MagentoFrameworkExceptionStateUserLockedException;
use MagentoFrameworkAppConfigScopeConfigInterface;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class LoginPost extends MagentoCustomerControllerAbstractAccount
{
/** @var AccountManagementInterface */
protected $customerAccountManagement;
/** @var Validator */
protected $formKeyValidator;
/**
* @var AccountRedirect
*/
protected $accountRedirect;
/**
* @var Session
*/
protected $session;
/**
* @var ScopeConfigInterface
*/
private $scopeConfig;
/**
* @var MagentoFrameworkStdlibCookieCookieMetadataFactory
*/
private $cookieMetadataFactory;
/**
* @var MagentoFrameworkStdlibCookiePhpCookieManager
*/
private $cookieMetadataManager;
/**
* @param Context $context
* @param Session $customerSession
* @param AccountManagementInterface $customerAccountManagement
* @param CustomerUrl $customerHelperData
* @param Validator $formKeyValidator
* @param AccountRedirect $accountRedirect
*/
public function __construct(
Context $context,
Session $customerSession,
AccountManagementInterface $customerAccountManagement,
CustomerUrl $customerHelperData,
Validator $formKeyValidator,
AccountRedirect $accountRedirect
) {
$this->session = $customerSession;
$this->customerAccountManagement = $customerAccountManagement;
$this->customerUrl = $customerHelperData;
$this->formKeyValidator = $formKeyValidator;
$this->accountRedirect = $accountRedirect;
parent::__construct($context);
}
/**
* Login post action
*
* @return MagentoFrameworkControllerResultRedirect
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function execute()
{
if ($this->session->isLoggedIn() || !$this->formKeyValidator->validate($this->getRequest())) {
/** @var MagentoFrameworkControllerResultRedirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('*/*/');
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();
if ($this->getCookieManager()->getCookie('mage-cache-sessid')) {
$metadata = $this->getCookieMetadataFactory()->createCookieMetadata();
$metadata->setPath('/');
$this->getCookieManager()->deleteCookie('mage-cache-sessid', $metadata);
}
$redirectUrl = $this->accountRedirect->getRedirectCookie();
if (!$this->getScopeConfig()->getValue('customer/startup/redirect_dashboard') && $redirectUrl) {
$this->accountRedirect->clearRedirectCookie();
$resultRedirect = $this->resultRedirectFactory->create();
// URL is checked to be internal in $this->_redirect->success()
$resultRedirect->setUrl($this->_redirect->success($redirectUrl));
return $resultRedirect;
}
} 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 (UserLockedException $e) {
$message = __(
'The account is locked. Please wait and try again or contact %1.',
$this->getScopeConfig()->getValue('contact/email/recipient_email')
);
$this->messageManager->addError($message);
$this->session->setUsername($login['username']);
} catch (AuthenticationException $e) {
if (isset($login['my_custom_page'])) {
$custom_redirect=true;
}
$message = __('Invalid login or password.');
$this->messageManager->addError($message);
$this->session->setUsername($login['username']);
} catch (LocalizedException $e) {
$message = $e->getMessage();
$this->messageManager->addError($message);
$this->session->setUsername($login['username']);
} catch (Exception $e) {
// PA DSS violation: throwing or logging an exception here can disclose customer password
$this->messageManager->addError(
__('An unspecified error occurred. Please contact us for assistance.')
);
}
} else {
$this->messageManager->addError(__('A login and a password are required.'));
}
}
if (isset($login['my_custom_page']) && $custom_redirect) {
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('mycustomlogin/index');
return $resultRedirect;
}
return $this->accountRedirect->getRedirect();
}
}
Please see the below image to better understand what I have added my logic:
Please see below is second image to written logic for redirect user to again if they enter wrong username or password:
Below is the main moto for override the controller:
I have created the custom login page for specific customer group, they will login from the custom design page, so I have created the mycustomlogin.phtml in my module like: appcodeVendorMyModuleviewfrontendtemplatesmycustomlogin.phtml
and passed the hidden input field value in the form for check where user had posted the form. I have get the hidden input value in AuthenticationException $e
to check. if user have posted the form from custom design.
Anyone can suggest me how I can add my logic into the Plugin file?
Thanks!
magento2 customer-account customer-group magento2.1.5 redirection
I am trying to overriding the below class in my custom module:
vendormagentomodule-customerControllerAccountLoginPost.php
For overriding this I have created the Plugin file as per the below Magento 2 official reference:
http://devdocs.magento.com/guides/v2.0/extension-dev-guide/plugins.html
I have created the app/code/Vendor/MyModule/etc/di.xml file:
<?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="MagentoCustomerControllerAccountLoginPost">
<plugin name="MyCucomtLoginAccountLoginPost" type="VendorMyModulePluginCustomerLoginPost" sortOrder="10" disabled="false"/>
</type>
</config>
Now, I have written the below is aroundExecute method to achieve my customization:
appcodeVendorMyModulePluginCustomer
<?php
namespace VendorMyModulePluginCustomer;
class LoginPost
{
public function aroundExecute(MagentoCustomerControllerAccountLoginPost $subject, Closure $proceed)
{
//echo 'here'; die;
if (isset($login['press_room_page'])) {
$custom_redirect=true;
}
if (isset($login['press_room_page']) && $custom_redirect) {
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('mycustomlogin/index');
return $resultRedirect;
}
}
}
?>
The above code is working fine when I am print something "here" it means the file is override successfully.
Now I am adding my logic in the execute method below is original file of vendor which I have edited for testing purpose to check the logic work or not:
vendormagentomodule-customerControllerAccount
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace MagentoCustomerControllerAccount;
use MagentoCustomerModelAccountRedirect as AccountRedirect;
use MagentoFrameworkAppActionContext;
use MagentoCustomerModelSession;
use MagentoCustomerApiAccountManagementInterface;
use MagentoCustomerModelUrl as CustomerUrl;
use MagentoFrameworkExceptionEmailNotConfirmedException;
use MagentoFrameworkExceptionAuthenticationException;
use MagentoFrameworkDataFormFormKeyValidator;
use MagentoFrameworkExceptionLocalizedException;
use MagentoFrameworkExceptionStateUserLockedException;
use MagentoFrameworkAppConfigScopeConfigInterface;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class LoginPost extends MagentoCustomerControllerAbstractAccount
{
/** @var AccountManagementInterface */
protected $customerAccountManagement;
/** @var Validator */
protected $formKeyValidator;
/**
* @var AccountRedirect
*/
protected $accountRedirect;
/**
* @var Session
*/
protected $session;
/**
* @var ScopeConfigInterface
*/
private $scopeConfig;
/**
* @var MagentoFrameworkStdlibCookieCookieMetadataFactory
*/
private $cookieMetadataFactory;
/**
* @var MagentoFrameworkStdlibCookiePhpCookieManager
*/
private $cookieMetadataManager;
/**
* @param Context $context
* @param Session $customerSession
* @param AccountManagementInterface $customerAccountManagement
* @param CustomerUrl $customerHelperData
* @param Validator $formKeyValidator
* @param AccountRedirect $accountRedirect
*/
public function __construct(
Context $context,
Session $customerSession,
AccountManagementInterface $customerAccountManagement,
CustomerUrl $customerHelperData,
Validator $formKeyValidator,
AccountRedirect $accountRedirect
) {
$this->session = $customerSession;
$this->customerAccountManagement = $customerAccountManagement;
$this->customerUrl = $customerHelperData;
$this->formKeyValidator = $formKeyValidator;
$this->accountRedirect = $accountRedirect;
parent::__construct($context);
}
/**
* Login post action
*
* @return MagentoFrameworkControllerResultRedirect
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function execute()
{
if ($this->session->isLoggedIn() || !$this->formKeyValidator->validate($this->getRequest())) {
/** @var MagentoFrameworkControllerResultRedirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('*/*/');
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();
if ($this->getCookieManager()->getCookie('mage-cache-sessid')) {
$metadata = $this->getCookieMetadataFactory()->createCookieMetadata();
$metadata->setPath('/');
$this->getCookieManager()->deleteCookie('mage-cache-sessid', $metadata);
}
$redirectUrl = $this->accountRedirect->getRedirectCookie();
if (!$this->getScopeConfig()->getValue('customer/startup/redirect_dashboard') && $redirectUrl) {
$this->accountRedirect->clearRedirectCookie();
$resultRedirect = $this->resultRedirectFactory->create();
// URL is checked to be internal in $this->_redirect->success()
$resultRedirect->setUrl($this->_redirect->success($redirectUrl));
return $resultRedirect;
}
} 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 (UserLockedException $e) {
$message = __(
'The account is locked. Please wait and try again or contact %1.',
$this->getScopeConfig()->getValue('contact/email/recipient_email')
);
$this->messageManager->addError($message);
$this->session->setUsername($login['username']);
} catch (AuthenticationException $e) {
if (isset($login['my_custom_page'])) {
$custom_redirect=true;
}
$message = __('Invalid login or password.');
$this->messageManager->addError($message);
$this->session->setUsername($login['username']);
} catch (LocalizedException $e) {
$message = $e->getMessage();
$this->messageManager->addError($message);
$this->session->setUsername($login['username']);
} catch (Exception $e) {
// PA DSS violation: throwing or logging an exception here can disclose customer password
$this->messageManager->addError(
__('An unspecified error occurred. Please contact us for assistance.')
);
}
} else {
$this->messageManager->addError(__('A login and a password are required.'));
}
}
if (isset($login['my_custom_page']) && $custom_redirect) {
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('mycustomlogin/index');
return $resultRedirect;
}
return $this->accountRedirect->getRedirect();
}
}
Please see the below image to better understand what I have added my logic:
Please see below is second image to written logic for redirect user to again if they enter wrong username or password:
Below is the main moto for override the controller:
I have created the custom login page for specific customer group, they will login from the custom design page, so I have created the mycustomlogin.phtml in my module like: appcodeVendorMyModuleviewfrontendtemplatesmycustomlogin.phtml
and passed the hidden input field value in the form for check where user had posted the form. I have get the hidden input value in AuthenticationException $e
to check. if user have posted the form from custom design.
Anyone can suggest me how I can add my logic into the Plugin file?
Thanks!
magento2 customer-account customer-group magento2.1.5 redirection
magento2 customer-account customer-group magento2.1.5 redirection
edited Jun 19 '17 at 9:42
mageDev0688
asked Jun 19 '17 at 6:20
mageDev0688mageDev0688
345725
345725
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
You need to rewrite the customer loginPost plugin file as per the below
<?php
namespace VENDORMYMODULENAMEPluginCustomer;
class LoginPost
{
public function __construct(
MagentoFrameworkAppActionContext $context
) {
$this->_request = $context->getRequest();
$this->_response = $context->getResponse();
$this->resultRedirectFactory = $context->getResultRedirectFactory();
$this->resultFactory = $context->getResultFactory();
}
public function aroundExecute(MagentoCustomerControllerAccountLoginPost $subject, $proceed)
{
$login = $this->_request->getPost('login');
$custom_redirect= false;
$returnValue = $proceed();
if (isset($login['press_room_page'])) {
$custom_redirect=true;
}
if (isset($login['press_room_page']) && $custom_redirect) {
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('mycustomlogin/index');
return $resultRedirect;
}
return $returnValue;
}
}
Welcome to Magento SE and thanks for your contribution, hope it helps the question author. A good answer that educates the reader would also explain what the mistakes were. What did you change to make the code work and why?
– Fabian Schmengler
Jun 19 '17 at 13:53
I have called the parent controller execute method. $returnValue = $proceed();
– lavanya
Jun 19 '17 at 15:54
superb lavanya - its working perfectly
– jassi
Jun 20 '17 at 5:26
It's very simple and working perfectly in all cases, I have tested it will both cases for custom login and default login. It's working fine.
– mageDev0688
Jun 20 '17 at 5:38
Sure @mageDev0688
– lavanya
Jun 22 '17 at 9:34
|
show 2 more comments
You can use after execute plugin and after that you can check either customer is login or not if not than you can redirect to your press page url
namespace VendorMyModulePluginCustomer;
class LoginPost
{
public function afterExecute(MagentoCustomerControllerAccountLoginPost $subject,$result)
{
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$customerSession = $objectManager->get('MagentoCustomerModelSession');
if(!$customerSession->isLoggedIn()) {
// redirect to press page
}
}
}
Thanks, but I want to check first if they entered wrong login details, then? Also, If they have entered the wrong detail then they will automatically redirect on the default magento login page. So, that's why I want to override theLoginpost.php
class? I just want to if they have filled the login details on my custom login page than I want to show the error message onInvalid Username or Password
on my custom login page not on the default login page.
– mageDev0688
Jun 19 '17 at 7:33
@Ankit could you please help me out this issue. This is not correct answer of the above question.
– mageDev0688
Jun 19 '17 at 8:38
please see the above answer, what I want.
– mageDev0688
Jun 20 '17 at 5:40
add a comment |
According to @lavanya answer, the best way indeed is to use the plugin interceptor, I wanted to add a full answer and in more clean.
app/code/Vendor/Module/etc/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="MagentoCustomerControllerAccountLoginPost">
<plugin name="CustomerLoginRedirectPlugin" type="VendorModulePluginLoginPost" disabled="false" sortOrder="1" />
</type>
</config>
app/code/Vendor/Module/Plugin/LoginPost.php
<?php
namespace VendorModulePlugin;
use MagentoFrameworkControllerResultFactory;
use MagentoFrameworkUrlInterface;
class LoginPost
{
protected $resultFactory;
protected $url;
protected $_request;
protected $_response;
public function __construct(
MagentoFrameworkAppActionContext $context,
UrlInterface $url,
ResultFactory $resultFactory
)
{
$this->_request = $context->getRequest();
$this->_response = $context->getResponse();
$this->url = $url;
$this->resultFactory = $resultFactory;
}
public function aroundExecute(MagentoCustomerControllerAccountLoginPost $subject, Closure $proceed) {
/*Execute code before the original function*/
$login = $this->_request->getPost('login');
//$username = $login['username']; to retrieve the user name for exemple.
$custom_redirect= false;
if (isset($login['press_room_page'])) {
$custom_redirect=true;
}
$resultProceed = $proceed(); // Original function
/*Execute code after the original function*/
if ($custom_redirect) {
$result = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$result->setUrl($this->url->getUrl('router/controllerfolder/controllername/'));
return $result;
}
return $resultProceed;
}
}
it could help someone.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f179636%2fmagento-2-override-the-cutomer-account-loginpost-controller%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to rewrite the customer loginPost plugin file as per the below
<?php
namespace VENDORMYMODULENAMEPluginCustomer;
class LoginPost
{
public function __construct(
MagentoFrameworkAppActionContext $context
) {
$this->_request = $context->getRequest();
$this->_response = $context->getResponse();
$this->resultRedirectFactory = $context->getResultRedirectFactory();
$this->resultFactory = $context->getResultFactory();
}
public function aroundExecute(MagentoCustomerControllerAccountLoginPost $subject, $proceed)
{
$login = $this->_request->getPost('login');
$custom_redirect= false;
$returnValue = $proceed();
if (isset($login['press_room_page'])) {
$custom_redirect=true;
}
if (isset($login['press_room_page']) && $custom_redirect) {
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('mycustomlogin/index');
return $resultRedirect;
}
return $returnValue;
}
}
Welcome to Magento SE and thanks for your contribution, hope it helps the question author. A good answer that educates the reader would also explain what the mistakes were. What did you change to make the code work and why?
– Fabian Schmengler
Jun 19 '17 at 13:53
I have called the parent controller execute method. $returnValue = $proceed();
– lavanya
Jun 19 '17 at 15:54
superb lavanya - its working perfectly
– jassi
Jun 20 '17 at 5:26
It's very simple and working perfectly in all cases, I have tested it will both cases for custom login and default login. It's working fine.
– mageDev0688
Jun 20 '17 at 5:38
Sure @mageDev0688
– lavanya
Jun 22 '17 at 9:34
|
show 2 more comments
You need to rewrite the customer loginPost plugin file as per the below
<?php
namespace VENDORMYMODULENAMEPluginCustomer;
class LoginPost
{
public function __construct(
MagentoFrameworkAppActionContext $context
) {
$this->_request = $context->getRequest();
$this->_response = $context->getResponse();
$this->resultRedirectFactory = $context->getResultRedirectFactory();
$this->resultFactory = $context->getResultFactory();
}
public function aroundExecute(MagentoCustomerControllerAccountLoginPost $subject, $proceed)
{
$login = $this->_request->getPost('login');
$custom_redirect= false;
$returnValue = $proceed();
if (isset($login['press_room_page'])) {
$custom_redirect=true;
}
if (isset($login['press_room_page']) && $custom_redirect) {
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('mycustomlogin/index');
return $resultRedirect;
}
return $returnValue;
}
}
Welcome to Magento SE and thanks for your contribution, hope it helps the question author. A good answer that educates the reader would also explain what the mistakes were. What did you change to make the code work and why?
– Fabian Schmengler
Jun 19 '17 at 13:53
I have called the parent controller execute method. $returnValue = $proceed();
– lavanya
Jun 19 '17 at 15:54
superb lavanya - its working perfectly
– jassi
Jun 20 '17 at 5:26
It's very simple and working perfectly in all cases, I have tested it will both cases for custom login and default login. It's working fine.
– mageDev0688
Jun 20 '17 at 5:38
Sure @mageDev0688
– lavanya
Jun 22 '17 at 9:34
|
show 2 more comments
You need to rewrite the customer loginPost plugin file as per the below
<?php
namespace VENDORMYMODULENAMEPluginCustomer;
class LoginPost
{
public function __construct(
MagentoFrameworkAppActionContext $context
) {
$this->_request = $context->getRequest();
$this->_response = $context->getResponse();
$this->resultRedirectFactory = $context->getResultRedirectFactory();
$this->resultFactory = $context->getResultFactory();
}
public function aroundExecute(MagentoCustomerControllerAccountLoginPost $subject, $proceed)
{
$login = $this->_request->getPost('login');
$custom_redirect= false;
$returnValue = $proceed();
if (isset($login['press_room_page'])) {
$custom_redirect=true;
}
if (isset($login['press_room_page']) && $custom_redirect) {
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('mycustomlogin/index');
return $resultRedirect;
}
return $returnValue;
}
}
You need to rewrite the customer loginPost plugin file as per the below
<?php
namespace VENDORMYMODULENAMEPluginCustomer;
class LoginPost
{
public function __construct(
MagentoFrameworkAppActionContext $context
) {
$this->_request = $context->getRequest();
$this->_response = $context->getResponse();
$this->resultRedirectFactory = $context->getResultRedirectFactory();
$this->resultFactory = $context->getResultFactory();
}
public function aroundExecute(MagentoCustomerControllerAccountLoginPost $subject, $proceed)
{
$login = $this->_request->getPost('login');
$custom_redirect= false;
$returnValue = $proceed();
if (isset($login['press_room_page'])) {
$custom_redirect=true;
}
if (isset($login['press_room_page']) && $custom_redirect) {
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('mycustomlogin/index');
return $resultRedirect;
}
return $returnValue;
}
}
edited Jun 19 '17 at 15:54
answered Jun 19 '17 at 13:44
lavanyalavanya
1494
1494
Welcome to Magento SE and thanks for your contribution, hope it helps the question author. A good answer that educates the reader would also explain what the mistakes were. What did you change to make the code work and why?
– Fabian Schmengler
Jun 19 '17 at 13:53
I have called the parent controller execute method. $returnValue = $proceed();
– lavanya
Jun 19 '17 at 15:54
superb lavanya - its working perfectly
– jassi
Jun 20 '17 at 5:26
It's very simple and working perfectly in all cases, I have tested it will both cases for custom login and default login. It's working fine.
– mageDev0688
Jun 20 '17 at 5:38
Sure @mageDev0688
– lavanya
Jun 22 '17 at 9:34
|
show 2 more comments
Welcome to Magento SE and thanks for your contribution, hope it helps the question author. A good answer that educates the reader would also explain what the mistakes were. What did you change to make the code work and why?
– Fabian Schmengler
Jun 19 '17 at 13:53
I have called the parent controller execute method. $returnValue = $proceed();
– lavanya
Jun 19 '17 at 15:54
superb lavanya - its working perfectly
– jassi
Jun 20 '17 at 5:26
It's very simple and working perfectly in all cases, I have tested it will both cases for custom login and default login. It's working fine.
– mageDev0688
Jun 20 '17 at 5:38
Sure @mageDev0688
– lavanya
Jun 22 '17 at 9:34
Welcome to Magento SE and thanks for your contribution, hope it helps the question author. A good answer that educates the reader would also explain what the mistakes were. What did you change to make the code work and why?
– Fabian Schmengler
Jun 19 '17 at 13:53
Welcome to Magento SE and thanks for your contribution, hope it helps the question author. A good answer that educates the reader would also explain what the mistakes were. What did you change to make the code work and why?
– Fabian Schmengler
Jun 19 '17 at 13:53
I have called the parent controller execute method. $returnValue = $proceed();
– lavanya
Jun 19 '17 at 15:54
I have called the parent controller execute method. $returnValue = $proceed();
– lavanya
Jun 19 '17 at 15:54
superb lavanya - its working perfectly
– jassi
Jun 20 '17 at 5:26
superb lavanya - its working perfectly
– jassi
Jun 20 '17 at 5:26
It's very simple and working perfectly in all cases, I have tested it will both cases for custom login and default login. It's working fine.
– mageDev0688
Jun 20 '17 at 5:38
It's very simple and working perfectly in all cases, I have tested it will both cases for custom login and default login. It's working fine.
– mageDev0688
Jun 20 '17 at 5:38
Sure @mageDev0688
– lavanya
Jun 22 '17 at 9:34
Sure @mageDev0688
– lavanya
Jun 22 '17 at 9:34
|
show 2 more comments
You can use after execute plugin and after that you can check either customer is login or not if not than you can redirect to your press page url
namespace VendorMyModulePluginCustomer;
class LoginPost
{
public function afterExecute(MagentoCustomerControllerAccountLoginPost $subject,$result)
{
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$customerSession = $objectManager->get('MagentoCustomerModelSession');
if(!$customerSession->isLoggedIn()) {
// redirect to press page
}
}
}
Thanks, but I want to check first if they entered wrong login details, then? Also, If they have entered the wrong detail then they will automatically redirect on the default magento login page. So, that's why I want to override theLoginpost.php
class? I just want to if they have filled the login details on my custom login page than I want to show the error message onInvalid Username or Password
on my custom login page not on the default login page.
– mageDev0688
Jun 19 '17 at 7:33
@Ankit could you please help me out this issue. This is not correct answer of the above question.
– mageDev0688
Jun 19 '17 at 8:38
please see the above answer, what I want.
– mageDev0688
Jun 20 '17 at 5:40
add a comment |
You can use after execute plugin and after that you can check either customer is login or not if not than you can redirect to your press page url
namespace VendorMyModulePluginCustomer;
class LoginPost
{
public function afterExecute(MagentoCustomerControllerAccountLoginPost $subject,$result)
{
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$customerSession = $objectManager->get('MagentoCustomerModelSession');
if(!$customerSession->isLoggedIn()) {
// redirect to press page
}
}
}
Thanks, but I want to check first if they entered wrong login details, then? Also, If they have entered the wrong detail then they will automatically redirect on the default magento login page. So, that's why I want to override theLoginpost.php
class? I just want to if they have filled the login details on my custom login page than I want to show the error message onInvalid Username or Password
on my custom login page not on the default login page.
– mageDev0688
Jun 19 '17 at 7:33
@Ankit could you please help me out this issue. This is not correct answer of the above question.
– mageDev0688
Jun 19 '17 at 8:38
please see the above answer, what I want.
– mageDev0688
Jun 20 '17 at 5:40
add a comment |
You can use after execute plugin and after that you can check either customer is login or not if not than you can redirect to your press page url
namespace VendorMyModulePluginCustomer;
class LoginPost
{
public function afterExecute(MagentoCustomerControllerAccountLoginPost $subject,$result)
{
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$customerSession = $objectManager->get('MagentoCustomerModelSession');
if(!$customerSession->isLoggedIn()) {
// redirect to press page
}
}
}
You can use after execute plugin and after that you can check either customer is login or not if not than you can redirect to your press page url
namespace VendorMyModulePluginCustomer;
class LoginPost
{
public function afterExecute(MagentoCustomerControllerAccountLoginPost $subject,$result)
{
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$customerSession = $objectManager->get('MagentoCustomerModelSession');
if(!$customerSession->isLoggedIn()) {
// redirect to press page
}
}
}
edited Jun 19 '17 at 8:11
Ankit Shah
4,655861138
4,655861138
answered Jun 19 '17 at 7:27
Jagriti JoshiJagriti Joshi
474
474
Thanks, but I want to check first if they entered wrong login details, then? Also, If they have entered the wrong detail then they will automatically redirect on the default magento login page. So, that's why I want to override theLoginpost.php
class? I just want to if they have filled the login details on my custom login page than I want to show the error message onInvalid Username or Password
on my custom login page not on the default login page.
– mageDev0688
Jun 19 '17 at 7:33
@Ankit could you please help me out this issue. This is not correct answer of the above question.
– mageDev0688
Jun 19 '17 at 8:38
please see the above answer, what I want.
– mageDev0688
Jun 20 '17 at 5:40
add a comment |
Thanks, but I want to check first if they entered wrong login details, then? Also, If they have entered the wrong detail then they will automatically redirect on the default magento login page. So, that's why I want to override theLoginpost.php
class? I just want to if they have filled the login details on my custom login page than I want to show the error message onInvalid Username or Password
on my custom login page not on the default login page.
– mageDev0688
Jun 19 '17 at 7:33
@Ankit could you please help me out this issue. This is not correct answer of the above question.
– mageDev0688
Jun 19 '17 at 8:38
please see the above answer, what I want.
– mageDev0688
Jun 20 '17 at 5:40
Thanks, but I want to check first if they entered wrong login details, then? Also, If they have entered the wrong detail then they will automatically redirect on the default magento login page. So, that's why I want to override the
Loginpost.php
class? I just want to if they have filled the login details on my custom login page than I want to show the error message on Invalid Username or Password
on my custom login page not on the default login page.– mageDev0688
Jun 19 '17 at 7:33
Thanks, but I want to check first if they entered wrong login details, then? Also, If they have entered the wrong detail then they will automatically redirect on the default magento login page. So, that's why I want to override the
Loginpost.php
class? I just want to if they have filled the login details on my custom login page than I want to show the error message on Invalid Username or Password
on my custom login page not on the default login page.– mageDev0688
Jun 19 '17 at 7:33
@Ankit could you please help me out this issue. This is not correct answer of the above question.
– mageDev0688
Jun 19 '17 at 8:38
@Ankit could you please help me out this issue. This is not correct answer of the above question.
– mageDev0688
Jun 19 '17 at 8:38
please see the above answer, what I want.
– mageDev0688
Jun 20 '17 at 5:40
please see the above answer, what I want.
– mageDev0688
Jun 20 '17 at 5:40
add a comment |
According to @lavanya answer, the best way indeed is to use the plugin interceptor, I wanted to add a full answer and in more clean.
app/code/Vendor/Module/etc/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="MagentoCustomerControllerAccountLoginPost">
<plugin name="CustomerLoginRedirectPlugin" type="VendorModulePluginLoginPost" disabled="false" sortOrder="1" />
</type>
</config>
app/code/Vendor/Module/Plugin/LoginPost.php
<?php
namespace VendorModulePlugin;
use MagentoFrameworkControllerResultFactory;
use MagentoFrameworkUrlInterface;
class LoginPost
{
protected $resultFactory;
protected $url;
protected $_request;
protected $_response;
public function __construct(
MagentoFrameworkAppActionContext $context,
UrlInterface $url,
ResultFactory $resultFactory
)
{
$this->_request = $context->getRequest();
$this->_response = $context->getResponse();
$this->url = $url;
$this->resultFactory = $resultFactory;
}
public function aroundExecute(MagentoCustomerControllerAccountLoginPost $subject, Closure $proceed) {
/*Execute code before the original function*/
$login = $this->_request->getPost('login');
//$username = $login['username']; to retrieve the user name for exemple.
$custom_redirect= false;
if (isset($login['press_room_page'])) {
$custom_redirect=true;
}
$resultProceed = $proceed(); // Original function
/*Execute code after the original function*/
if ($custom_redirect) {
$result = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$result->setUrl($this->url->getUrl('router/controllerfolder/controllername/'));
return $result;
}
return $resultProceed;
}
}
it could help someone.
add a comment |
According to @lavanya answer, the best way indeed is to use the plugin interceptor, I wanted to add a full answer and in more clean.
app/code/Vendor/Module/etc/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="MagentoCustomerControllerAccountLoginPost">
<plugin name="CustomerLoginRedirectPlugin" type="VendorModulePluginLoginPost" disabled="false" sortOrder="1" />
</type>
</config>
app/code/Vendor/Module/Plugin/LoginPost.php
<?php
namespace VendorModulePlugin;
use MagentoFrameworkControllerResultFactory;
use MagentoFrameworkUrlInterface;
class LoginPost
{
protected $resultFactory;
protected $url;
protected $_request;
protected $_response;
public function __construct(
MagentoFrameworkAppActionContext $context,
UrlInterface $url,
ResultFactory $resultFactory
)
{
$this->_request = $context->getRequest();
$this->_response = $context->getResponse();
$this->url = $url;
$this->resultFactory = $resultFactory;
}
public function aroundExecute(MagentoCustomerControllerAccountLoginPost $subject, Closure $proceed) {
/*Execute code before the original function*/
$login = $this->_request->getPost('login');
//$username = $login['username']; to retrieve the user name for exemple.
$custom_redirect= false;
if (isset($login['press_room_page'])) {
$custom_redirect=true;
}
$resultProceed = $proceed(); // Original function
/*Execute code after the original function*/
if ($custom_redirect) {
$result = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$result->setUrl($this->url->getUrl('router/controllerfolder/controllername/'));
return $result;
}
return $resultProceed;
}
}
it could help someone.
add a comment |
According to @lavanya answer, the best way indeed is to use the plugin interceptor, I wanted to add a full answer and in more clean.
app/code/Vendor/Module/etc/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="MagentoCustomerControllerAccountLoginPost">
<plugin name="CustomerLoginRedirectPlugin" type="VendorModulePluginLoginPost" disabled="false" sortOrder="1" />
</type>
</config>
app/code/Vendor/Module/Plugin/LoginPost.php
<?php
namespace VendorModulePlugin;
use MagentoFrameworkControllerResultFactory;
use MagentoFrameworkUrlInterface;
class LoginPost
{
protected $resultFactory;
protected $url;
protected $_request;
protected $_response;
public function __construct(
MagentoFrameworkAppActionContext $context,
UrlInterface $url,
ResultFactory $resultFactory
)
{
$this->_request = $context->getRequest();
$this->_response = $context->getResponse();
$this->url = $url;
$this->resultFactory = $resultFactory;
}
public function aroundExecute(MagentoCustomerControllerAccountLoginPost $subject, Closure $proceed) {
/*Execute code before the original function*/
$login = $this->_request->getPost('login');
//$username = $login['username']; to retrieve the user name for exemple.
$custom_redirect= false;
if (isset($login['press_room_page'])) {
$custom_redirect=true;
}
$resultProceed = $proceed(); // Original function
/*Execute code after the original function*/
if ($custom_redirect) {
$result = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$result->setUrl($this->url->getUrl('router/controllerfolder/controllername/'));
return $result;
}
return $resultProceed;
}
}
it could help someone.
According to @lavanya answer, the best way indeed is to use the plugin interceptor, I wanted to add a full answer and in more clean.
app/code/Vendor/Module/etc/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="MagentoCustomerControllerAccountLoginPost">
<plugin name="CustomerLoginRedirectPlugin" type="VendorModulePluginLoginPost" disabled="false" sortOrder="1" />
</type>
</config>
app/code/Vendor/Module/Plugin/LoginPost.php
<?php
namespace VendorModulePlugin;
use MagentoFrameworkControllerResultFactory;
use MagentoFrameworkUrlInterface;
class LoginPost
{
protected $resultFactory;
protected $url;
protected $_request;
protected $_response;
public function __construct(
MagentoFrameworkAppActionContext $context,
UrlInterface $url,
ResultFactory $resultFactory
)
{
$this->_request = $context->getRequest();
$this->_response = $context->getResponse();
$this->url = $url;
$this->resultFactory = $resultFactory;
}
public function aroundExecute(MagentoCustomerControllerAccountLoginPost $subject, Closure $proceed) {
/*Execute code before the original function*/
$login = $this->_request->getPost('login');
//$username = $login['username']; to retrieve the user name for exemple.
$custom_redirect= false;
if (isset($login['press_room_page'])) {
$custom_redirect=true;
}
$resultProceed = $proceed(); // Original function
/*Execute code after the original function*/
if ($custom_redirect) {
$result = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$result->setUrl($this->url->getUrl('router/controllerfolder/controllername/'));
return $result;
}
return $resultProceed;
}
}
it could help someone.
answered 2 days ago
PЯINCƏPЯINCƏ
7,75131137
7,75131137
add a comment |
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f179636%2fmagento-2-override-the-cutomer-account-loginpost-controller%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown