src/Security/LoginFormAuthenticator.php line 64

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\User;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  9. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  11. use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
  12. use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;
  13. use Symfony\Component\Security\Core\Security;
  14. use Symfony\Component\Security\Core\User\UserInterface;
  15. use Symfony\Component\Security\Core\User\UserProviderInterface;
  16. use Symfony\Component\Security\Csrf\CsrfToken;
  17. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  18. use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator;
  19. use Symfony\Component\Security\Guard\PasswordAuthenticatedInterface;
  20. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  21. class LoginFormAuthenticator extends AbstractFormLoginAuthenticator implements PasswordAuthenticatedInterface {
  22.     use TargetPathTrait;
  23.     public const LOGIN_ROUTE 'app_login';
  24.     private $entityManager;
  25.     private $urlGenerator;
  26.     private $csrfTokenManager;
  27.     private $passwordEncoder;
  28.     public function __construct(EntityManagerInterface $entityManagerUrlGeneratorInterface $urlGeneratorCsrfTokenManagerInterface $csrfTokenManagerUserPasswordEncoderInterface $passwordEncoder) {
  29.         $this->entityManager $entityManager;
  30.         $this->urlGenerator $urlGenerator;
  31.         $this->csrfTokenManager $csrfTokenManager;
  32.         $this->passwordEncoder $passwordEncoder;
  33.     }
  34.     public function supports(Request $request): bool {
  35.         return self::LOGIN_ROUTE === $request->attributes->get('_route') && $request->isMethod('POST');
  36.     }
  37.     public function getCredentials(Request $request) {
  38.         $credentials = [
  39.             'username' => $request->request->get('username'),
  40.             'email' => $request->request->get('email'),
  41.             'password' => $request->request->get('password'),
  42.             'csrf_token' => $request->request->get('_csrf_token'),
  43.         ];
  44.         $request->getSession()->set(
  45.                 Security::LAST_USERNAME,
  46.                 $credentials['email']
  47.         );
  48.         return $credentials;
  49.     }
  50.     public function getUser($credentialsUserProviderInterface $userProvider): ?User {
  51.         $token = new CsrfToken('authenticate'$credentials['csrf_token']);
  52.         if (!$this->csrfTokenManager->isTokenValid($token)) {
  53.             throw new InvalidCsrfTokenException();
  54.         }
  55.         $user $this->entityManager->getRepository(User::class)->findOneBy(['username' => $credentials['username']]);
  56.         if (!$user) {
  57.             // fail authentication with a custom error
  58.             throw new CustomUserMessageAuthenticationException('user could not be found.');
  59.         }
  60.         return $user;
  61.     }
  62.     public function checkCredentials($credentialsUserInterface $user): bool {
  63.         return $this->passwordEncoder->isPasswordValid($user$credentials['password']);
  64.     }
  65.     /**
  66.      * Used to upgrade (rehash) the user's password automatically over time.
  67.      */
  68.     public function getPassword($credentials): ?string {
  69.         return $credentials['password'];
  70.     }
  71.     public function onAuthenticationSuccess(Request $requestTokenInterface $token$providerKey): ?Response {
  72.         if ($targetPath $this->getTargetPath($request->getSession(), $providerKey)) {
  73.             return new RedirectResponse($targetPath);
  74.         }
  75.         if(!$token->getUser()->isVerfier()){
  76.               return new RedirectResponse($this->urlGenerator->generate('app_changePassword'));
  77.         }
  78.     
  79.         return new RedirectResponse($this->urlGenerator->generate('app_user'));
  80.            
  81.            
  82.         //  throw new \Exception('TODO: provide a valid redirect inside ' . __FILE__);
  83.     }
  84.     protected function getLoginUrl(): string {
  85.         return $this->urlGenerator->generate(self::LOGIN_ROUTE);
  86.     }
  87. }