src/Security/UsersAuthenticator.php line 27

  1. <?php
  2. namespace App\Security;
  3. use App\Entity\User;
  4. use Doctrine\ORM\EntityManager;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;
  12. use Symfony\Component\Security\Core\Exception\UserNotFoundException;
  13. use Symfony\Component\Security\Core\Security;
  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\Http\Authenticator\AbstractLoginFormAuthenticator;
  18. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;
  19. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  20. use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
  21. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  22. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  23. class UsersAuthenticator extends AbstractLoginFormAuthenticator
  24. {
  25.     use TargetPathTrait;
  26.     public const LOGIN_ROUTE 'app_login';
  27.     private UrlGeneratorInterface $urlGenerator;
  28.     private EntityManagerInterface $entityManager;
  29.     public function __construct(EntityManagerInterface $entityManagerUrlGeneratorInterface $urlGenerator)
  30.     {
  31.         $this->urlGenerator $urlGenerator;
  32.         $this->entityManager $entityManager;
  33.     }
  34.     public function authenticate(Request $request): Passport
  35.     {
  36.         $username $request->request->get('username''');
  37.         $request->getSession()->set(Security::LAST_USERNAME$username);
  38.         return new Passport(
  39.             new UserBadge($username),
  40.             new PasswordCredentials($request->request->get('password''')),
  41.             [
  42.                 new CsrfTokenBadge('authenticate'$request->request->get('_csrf_token')),
  43.             ]
  44.         );
  45.     }
  46.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $firewallName): ?Response
  47.     {
  48.         if ($targetPath $this->getTargetPath($request->getSession(), $firewallName)) {
  49.             return new RedirectResponse($targetPath);
  50.         }
  51.         return new RedirectResponse($this->urlGenerator->generate('app_dashboard'));
  52.     }
  53.     protected function getLoginUrl(Request $request): string
  54.     {
  55.         return $this->urlGenerator->generate(self::LOGIN_ROUTE);
  56.     }
  57. }