src/Security/LoginFormAuthenticator.php line 65

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