src/Security/LoginFormAuthenticator.php line 20

  1. <?php
  2. namespace App\Security;
  3. use App\ActivityLogger\ActivityLogger;
  4. use Symfony\Component\HttpFoundation\RedirectResponse;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  10. use Symfony\Component\Security\Core\Security;
  11. use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;
  12. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;
  13. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  14. use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
  15. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  16. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  17. class LoginFormAuthenticator extends AbstractLoginFormAuthenticator
  18. {
  19.     use TargetPathTrait;
  20.     public const LOGIN_ROUTE 'app_login';
  21.     private UrlGeneratorInterface $urlGenerator;
  22.     private ActivityLogger $activityLogger;
  23.     public function __construct(UrlGeneratorInterface $urlGeneratorActivityLogger $activityLogger)
  24.     {
  25.         $this->urlGenerator $urlGenerator;
  26.         $this->activityLogger $activityLogger;
  27.     }
  28.     public function authenticate(Request $request): Passport
  29.     {
  30.         $email $request->request->get('email''');
  31.         $request->getSession()->set(Security::LAST_USERNAME$email);
  32.         return new Passport(
  33.             new UserBadge($email),
  34.             new PasswordCredentials($request->request->get('password''')),
  35.             [
  36.                 new CsrfTokenBadge('authenticate'$request->request->get('_csrf_token')),
  37.             ]
  38.         );
  39.     }
  40.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $firewallName): ?Response
  41.     {
  42.         $this->activityLogger->logLogin($token->getUserIdentifier());
  43.         // For example:
  44.         $mainTableau $token->getUser()->getTableaux()->first();
  45.         if($mainTableau){
  46.             return new RedirectResponse($this->urlGenerator->generate('tableau', ['id' => $mainTableau->getId()]));
  47.         }
  48.         if ($targetPath $this->getTargetPath($request->getSession(), $firewallName)) {
  49.             return new RedirectResponse($targetPath);
  50.         }
  51.         return new RedirectResponse($this->urlGenerator->generate('homepage'));
  52. //        throw new \Exception('TODO: provide a valid redirect inside '.__FILE__);
  53.     }
  54.     public function onAuthenticationFailure(Request $requestAuthenticationException $exception): Response
  55.     {
  56.         return parent::onAuthenticationFailure($request$exception);
  57.     }
  58.     protected function getLoginUrl(Request $request): string
  59.     {
  60.         return $this->urlGenerator->generate(self::LOGIN_ROUTE);
  61.     }
  62. }