/** * Perform user login. On a successful login, the user is redirected to the URL * found in the success_url form parameter, or / if none was found. * * If login is not successfull, the same view is displayed again. * */ public function loginAction() { if (!$this->getSecurityContext()->hasCustomerUser()) { $message = false; $request = $this->getRequest(); $customerLoginForm = new CustomerLogin($request); try { $form = $this->validateForm($customerLoginForm, "post"); // If User is a new customer if ($form->get('account')->getData() == 0 && !$form->get("email")->getErrors()) { return $this->generateRedirectFromRoute("customer.create.process", array("email" => $form->get("email")->getData())); } else { try { $authenticator = new CustomerUsernamePasswordFormAuthenticator($request, $customerLoginForm); $customer = $authenticator->getAuthentifiedUser(); $this->processLogin($customer); if (intval($form->get('remember_me')->getData()) > 0) { // If a remember me field if present and set in the form, create // the cookie thant store "remember me" information $this->createRememberMeCookie($customer, $this->getRememberMeCookieName(), $this->getRememberMeCookieExpiration()); } return $this->generateSuccessRedirect($customerLoginForm); } catch (UsernameNotFoundException $e) { $message = $this->getTranslator()->trans("Wrong email or password. Please try again", [], Front::MESSAGE_DOMAIN); } catch (WrongPasswordException $e) { $message = $this->getTranslator()->trans("Wrong email or password. Please try again", [], Front::MESSAGE_DOMAIN); } catch (AuthenticationException $e) { $message = $this->getTranslator()->trans("Wrong email or password. Please try again", [], Front::MESSAGE_DOMAIN); } } } catch (FormValidationException $e) { $message = $this->getTranslator()->trans("Please check your input: %s", ['%s' => $e->getMessage()], Front::MESSAGE_DOMAIN); } catch (\Exception $e) { $message = $this->getTranslator()->trans("Sorry, an error occured: %s", ['%s' => $e->getMessage()], Front::MESSAGE_DOMAIN); } if ($message !== false) { Tlog::getInstance()->error(sprintf("Error during customer login process : %s. Exception was %s", $message, $e->getMessage())); $customerLoginForm->setErrorMessage($message); $this->getParserContext()->addForm($customerLoginForm); } } }
/** * @return \Symfony\Component\HttpFoundation\Response|JsonResponse * * Get a customer given its email and password. * @author Baptiste Cabarrou <*****@*****.**> */ public function checkLoginAction() { $this->checkAuth($this->resources, $this->modules, AccessManager::VIEW); $request = $this->getRequest(); $customerLoginForm = $this->createForm(ApiForm::CUSTOMER_LOGIN); try { $this->validateForm($customerLoginForm, "post"); $authenticator = new CustomerUsernamePasswordFormAuthenticator($request, $customerLoginForm); /** @var UserInterface $customer */ $customer = $authenticator->getAuthentifiedUser(); return $this->getAction($customer->getId()); } catch (UsernameNotFoundException $e) { return new JsonResponse(["error" => $e->getMessage()], 404); } catch (WrongPasswordException $e) { return new JsonResponse(["error" => $e->getMessage()], 404); } catch (HttpException $e) { return new JsonResponse(["error" => $e->getMessage()], $e->getStatusCode()); } catch (\Exception $e) { return new JsonResponse(["error" => $e->getMessage()], 500); } }