src/Controller/SecurityController.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Service\LoginAttemptService;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  8. class SecurityController extends AbstractController
  9. {
  10.     private $loginAttemptService;
  11.     public function __construct(LoginAttemptService $loginAttemptService)
  12.     {
  13.         $this->loginAttemptService $loginAttemptService;
  14.     }
  15.     /**
  16.      * @Route("/login", name="app_login")
  17.      */
  18.     public function login(AuthenticationUtils $authenticationUtils): Response
  19.     {
  20.         if ($this->getUser()) {
  21.             return $this->redirectToRoute('app_dashboard');
  22.         }
  23.         $error $authenticationUtils->getLastAuthenticationError();
  24.         $lastUsername $authenticationUtils->getLastUsername();
  25.         $errorMessage null;
  26.         $remainingAttempts null;
  27.         if ($error) {
  28.             $errorMessage 'Email ou mot de passe incorrect.';
  29.             
  30.             if ($this->getUser()) {
  31.                 $user $this->getUser();
  32.                 $remainingAttempts $this->loginAttemptService->getRemainingAttempts($user);
  33.                 
  34.                 if ($remainingAttempts 0) {
  35.                     $errorMessage .= sprintf(' Il vous reste %d tentative(s).'$remainingAttempts);
  36.                 }
  37.             }
  38.         }
  39.         return $this->render('security/login.html.twig', [
  40.             'last_username' => $lastUsername,
  41.             'error' => $errorMessage,
  42.             'remaining_attempts' => $remainingAttempts,
  43.         ]);
  44.     }
  45.     /**
  46.      * @Route("/logout", name="app_logout")
  47.      */
  48.     public function logout(): void
  49.     {
  50.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  51.     }
  52. }