Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Login.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
14 use Magento\Customer\Model\Account\Redirect as AccountRedirect;
19 
27 class Login extends \Magento\Framework\App\Action\Action implements HttpPostActionInterface
28 {
32  protected $session;
33 
38 
42  protected $helper;
43 
47  protected $resultJsonFactory;
48 
52  protected $resultRawFactory;
53 
57  protected $accountRedirect;
58 
62  protected $scopeConfig;
63 
67  private $cookieManager;
68 
72  private $cookieMetadataFactory;
73 
86  public function __construct(
87  \Magento\Framework\App\Action\Context $context,
88  \Magento\Customer\Model\Session $customerSession,
89  \Magento\Framework\Json\Helper\Data $helper,
91  \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
92  \Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
93  CookieManagerInterface $cookieManager = null,
94  CookieMetadataFactory $cookieMetadataFactory = null
95  ) {
96  parent::__construct($context);
97  $this->customerSession = $customerSession;
98  $this->helper = $helper;
99  $this->customerAccountManagement = $customerAccountManagement;
100  $this->resultJsonFactory = $resultJsonFactory;
101  $this->resultRawFactory = $resultRawFactory;
102  $this->cookieManager = $cookieManager ?:
103  ObjectManager::getInstance()->get(CookieManagerInterface::class);
104  $this->cookieMetadataFactory = $cookieMetadataFactory ?:
105  ObjectManager::getInstance()->get(CookieMetadataFactory::class);
106  }
107 
115  protected function getAccountRedirect()
116  {
117  if (!is_object($this->accountRedirect)) {
118  $this->accountRedirect = ObjectManager::getInstance()->get(AccountRedirect::class);
119  }
120  return $this->accountRedirect;
121  }
122 
130  public function setAccountRedirect($value)
131  {
132  $this->accountRedirect = $value;
133  }
134 
139  protected function getScopeConfig()
140  {
141  if (!is_object($this->scopeConfig)) {
142  $this->scopeConfig = ObjectManager::getInstance()->get(ScopeConfigInterface::class);
143  }
144  return $this->scopeConfig;
145  }
146 
152  public function setScopeConfig($value)
153  {
154  $this->scopeConfig = $value;
155  }
156 
165  public function execute()
166  {
167  $credentials = null;
168  $httpBadRequestCode = 400;
169 
171  $resultRaw = $this->resultRawFactory->create();
172  try {
173  $credentials = $this->helper->jsonDecode($this->getRequest()->getContent());
174  } catch (\Exception $e) {
175  return $resultRaw->setHttpResponseCode($httpBadRequestCode);
176  }
177  if (!$credentials || $this->getRequest()->getMethod() !== 'POST' || !$this->getRequest()->isXmlHttpRequest()) {
178  return $resultRaw->setHttpResponseCode($httpBadRequestCode);
179  }
180 
181  $response = [
182  'errors' => false,
183  'message' => __('Login successful.')
184  ];
185  try {
186  $customer = $this->customerAccountManagement->authenticate(
187  $credentials['username'],
188  $credentials['password']
189  );
190  $this->customerSession->setCustomerDataAsLoggedIn($customer);
191  $this->customerSession->regenerateId();
192  $redirectRoute = $this->getAccountRedirect()->getRedirectCookie();
193  if ($this->cookieManager->getCookie('mage-cache-sessid')) {
194  $metadata = $this->cookieMetadataFactory->createCookieMetadata();
195  $metadata->setPath('/');
196  $this->cookieManager->deleteCookie('mage-cache-sessid', $metadata);
197  }
198  if (!$this->getScopeConfig()->getValue('customer/startup/redirect_dashboard') && $redirectRoute) {
199  $response['redirectUrl'] = $this->_redirect->success($redirectRoute);
200  $this->getAccountRedirect()->clearRedirectCookie();
201  }
202  } catch (EmailNotConfirmedException $e) {
203  $response = [
204  'errors' => true,
205  'message' => $e->getMessage()
206  ];
207  } catch (InvalidEmailOrPasswordException $e) {
208  $response = [
209  'errors' => true,
210  'message' => $e->getMessage()
211  ];
212  } catch (LocalizedException $e) {
213  $response = [
214  'errors' => true,
215  'message' => $e->getMessage()
216  ];
217  } catch (\Exception $e) {
218  $response = [
219  'errors' => true,
220  'message' => __('Invalid login or password.')
221  ];
222  }
224  $resultJson = $this->resultJsonFactory->create();
225  return $resultJson->setData($response);
226  }
227 }
$response
Definition: 404.php:11
_redirect($path, $arguments=[])
Definition: Action.php:167
$customer
Definition: customers.php:11
__construct(\Magento\Framework\App\Action\Context $context, \Magento\Customer\Model\Session $customerSession, \Magento\Framework\Json\Helper\Data $helper, AccountManagementInterface $customerAccountManagement, \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory, \Magento\Framework\Controller\Result\RawFactory $resultRawFactory, CookieManagerInterface $cookieManager=null, CookieMetadataFactory $cookieMetadataFactory=null)
Definition: Login.php:86
__()
Definition: __.php:13
$value
Definition: gender.phtml:16