src/Security/LoginFormAuthenticator.php line 23

  1. <?php
  2. namespace App\Security;
  3. use App\Entity\User;
  4. use App\Repository\UserRepository;
  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\Routing\RouterInterface;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  12. use Symfony\Component\Security\Core\Exception\UserNotFoundException;
  13. use Symfony\Component\Security\Core\Security;
  14. use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;
  15. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  16. use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\CustomCredentials;
  17. use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
  18. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  19. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  20. class LoginFormAuthenticator extends AbstractLoginFormAuthenticator
  21. {
  22.     use TargetPathTrait;
  23.     public const LOGIN_ROUTE 'app_login';
  24.     public function __construct(
  25.         private readonly UrlGeneratorInterface $urlGenerator,
  26.         private readonly UserRepository $userRepository,
  27.         private readonly RouterInterface $router
  28.     )
  29.     {}
  30.     public function authenticate(Request $request): Passport
  31.     {
  32.         $email $request->request->get('email''');
  33.         $password $request->request->get('password''');
  34.         //$request->getSession()->set(Security::LAST_USERNAME, $email);
  35.         /*
  36.         return new Passport(
  37.             new UserBadge($email),
  38.             new PasswordCredentials($password),
  39.             [
  40.                 new CsrfTokenBadge('authenticate', $request->request->get('_csrf_token')),
  41.             ]
  42.         );*/
  43.         return new Passport(
  44.             new UserBadge($email, function($userIdentifier) {
  45.                 // Optionally pass a callback to load the User manually
  46.                 $user $this->userRepository->findOneBy(['email' => $userIdentifier]);
  47.                 if (!$user) {
  48.                     throw new UserNotFoundException();
  49.                 }
  50.                 return $user;
  51.             }),
  52.             new PasswordCredentials($password)
  53.         );
  54.     }
  55.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $firewallName): ?Response
  56.     {
  57.         if ($targetPath $this->getTargetPath($request->getSession(), $firewallName)) {
  58.             return new RedirectResponse($targetPath);
  59.         }
  60.         // For example:
  61.         return new RedirectResponse($this->urlGenerator->generate('app_index'));
  62.     }
  63.     public function onAuthenticationFailure(Request $requestAuthenticationException $exception): Response
  64.     {
  65.         $request->getSession()->set(Security::AUTHENTICATION_ERROR$exception);
  66.         return new RedirectResponse(
  67.             $this->router->generate('app_login')
  68.         );
  69.     }
  70.     protected function getLoginUrl(Request $request): string
  71.     {
  72.         return $this->urlGenerator->generate(self::LOGIN_ROUTE);
  73.     }
  74. }