/** * {@inheritdoc} */ protected function checkAuthentication(UserInterface $user, UsernamePasswordToken $token) { $currentUser = $token->getUser(); if ($currentUser instanceof UserInterface) { if ($currentUser->getPassword() !== $user->getPassword()) { throw new BadCredentialsException('The credentials were changed from another session.'); } } else { if (!($presentedPassword = $token->getCredentials())) { throw new BadCredentialsException('The presented password cannot be empty.'); } $client = $this->client; $cartId = null; if ($user instanceof User) { $cartId = $user->getCartId(); } $request = CustomerLoginRequest::ofEmailAndPassword($token->getUser(), $presentedPassword, $cartId); $response = $request->executeWithClient($client); if ($response->isError()) { throw new BadCredentialsException('The presented password is invalid.'); } $result = $request->mapResponse($response); $customer = $result->getCustomer(); if ($currentUser !== $customer->getEmail()) { throw new BadCredentialsException('The presented password is invalid.'); } if ($user instanceof User) { $user->setId($customer->getId()); $cart = $result->getCart(); if (!is_null($cart)) { $user->setCartId($cart->getId()); $user->setCartItemCount($cart->getLineItemCount()); } } } }