customerSession = $customerSession; $this->helper = $helper; $this->customerAccountManagement = $customerAccountManagement; $this->resultJsonFactory = $resultJsonFactory; $this->resultRawFactory = $resultRawFactory; $this->cookieManager = $cookieManager ?: ObjectManager::getInstance()->get(CookieManagerInterface::class); $this->cookieMetadataFactory = $cookieMetadataFactory ?: ObjectManager::getInstance()->get(CookieMetadataFactory::class); } /** * Get account redirect. * * @deprecated 100.0.10 * @return AccountRedirect */ protected function getAccountRedirect() { if (!is_object($this->accountRedirect)) { $this->accountRedirect = ObjectManager::getInstance()->get(AccountRedirect::class); } return $this->accountRedirect; } /** * Account redirect setter for unit tests. * * @deprecated 100.0.10 * @param AccountRedirect $value * @return void */ public function setAccountRedirect($value) { $this->accountRedirect = $value; } /** * Initializes config dependency. * * @deprecated 100.0.10 * @return ScopeConfigInterface */ protected function getScopeConfig() { if (!is_object($this->scopeConfig)) { $this->scopeConfig = ObjectManager::getInstance()->get(ScopeConfigInterface::class); } return $this->scopeConfig; } /** * Sets config dependency. * * @deprecated 100.0.10 * @param ScopeConfigInterface $value * @return void */ public function setScopeConfig($value) { $this->scopeConfig = $value; } /** * Login registered users and initiate a session. * * Expects a POST. ex for JSON {"username":"user@magento.com", "password":"userpassword"} * * @return \Magento\Framework\Controller\ResultInterface * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function execute() { $credentials = null; $httpBadRequestCode = 400; /** @var \Magento\Framework\Controller\Result\Raw $resultRaw */ $resultRaw = $this->resultRawFactory->create(); try { $credentials = $this->helper->jsonDecode($this->getRequest()->getContent()); } catch (\Exception $e) { return $resultRaw->setHttpResponseCode($httpBadRequestCode); } if (!$credentials || $this->getRequest()->getMethod() !== 'POST' || !$this->getRequest()->isXmlHttpRequest()) { return $resultRaw->setHttpResponseCode($httpBadRequestCode); } $response = [ 'errors' => false, 'message' => __('Login successful.') ]; try { $customer = $this->customerAccountManagement->authenticate( $credentials['username'], $credentials['password'] ); $this->customerSession->setCustomerDataAsLoggedIn($customer); $this->customerSession->regenerateId(); $redirectRoute = $this->getAccountRedirect()->getRedirectCookie(); if ($this->cookieManager->getCookie('mage-cache-sessid')) { $metadata = $this->cookieMetadataFactory->createCookieMetadata(); $metadata->setPath('/'); $this->cookieManager->deleteCookie('mage-cache-sessid', $metadata); } if (!$this->getScopeConfig()->getValue('customer/startup/redirect_dashboard') && $redirectRoute) { $response['redirectUrl'] = $this->_redirect->success($redirectRoute); $this->getAccountRedirect()->clearRedirectCookie(); } } catch (LocalizedException $e) { $response = [ 'errors' => true, 'message' => $e->getMessage(), ]; } catch (\Exception $e) { $response = [ 'errors' => true, 'message' => __('Invalid login or password.'), ]; } /** @var \Magento\Framework\Controller\Result\Json $resultJson */ $resultJson = $this->resultJsonFactory->create(); return $resultJson->setData($response); } }