session = $customerSession; $this->customerAccountManagement = $customerAccountManagement; $this->customerUrl = $customerHelperData; $this->formKeyValidator = $formKeyValidator; $this->accountRedirect = $accountRedirect; parent::__construct($context); } /** * Get scope config * * @return ScopeConfigInterface * @deprecated 100.0.10 */ private function getScopeConfig() { if (!($this->scopeConfig instanceof \Magento\Framework\App\Config\ScopeConfigInterface)) { return \Magento\Framework\App\ObjectManager::getInstance()->get( \Magento\Framework\App\Config\ScopeConfigInterface::class ); } else { return $this->scopeConfig; } } /** * Retrieve cookie manager * * @deprecated 100.1.0 * @return \Magento\Framework\Stdlib\Cookie\PhpCookieManager */ private function getCookieManager() { if (!$this->cookieMetadataManager) { $this->cookieMetadataManager = \Magento\Framework\App\ObjectManager::getInstance()->get( \Magento\Framework\Stdlib\Cookie\PhpCookieManager::class ); } return $this->cookieMetadataManager; } /** * Retrieve cookie metadata factory * * @deprecated 100.1.0 * @return \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory */ private function getCookieMetadataFactory() { if (!$this->cookieMetadataFactory) { $this->cookieMetadataFactory = \Magento\Framework\App\ObjectManager::getInstance()->get( \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class ); } return $this->cookieMetadataFactory; } /** * @inheritDoc */ public function createCsrfValidationException( RequestInterface $request ): ?InvalidRequestException { /** @var Redirect $resultRedirect */ $resultRedirect = $this->resultRedirectFactory->create(); $resultRedirect->setPath('*/*/'); return new InvalidRequestException( $resultRedirect, [new Phrase('Invalid Form Key. Please refresh the page.')] ); } /** * @inheritDoc */ public function validateForCsrf(RequestInterface $request): ?bool { return null; } /** * Login post action * * @return \Magento\Framework\Controller\Result\Redirect * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ 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('*/*/'); 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. Click here to resend confirmation email.', $value ); } catch (UserLockedException $e) { $message = __( 'The account sign-in was incorrect or your account is disabled temporarily. ' . 'Please wait and try again later.' ); } catch (AuthenticationException $e) { $message = __( 'The account sign-in was incorrect or your account is disabled temporarily. ' . 'Please wait and try again later.' ); } catch (LocalizedException $e) { $message = $e->getMessage(); } 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.') ); } finally { if (isset($message)) { $this->messageManager->addError($message); $this->session->setUsername($login['username']); } } } else { $this->messageManager->addError(__('A login and a password are required.')); } } return $this->accountRedirect->getRedirect(); } }