src/Controller/Application/SecurityController.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Application;
  3. use Symfony\Component\HttpFoundation\Response;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  8. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  9. use Symfony\Contracts\Translation\TranslatorInterface;
  10. use Symfony\Component\Security\Core\Security;
  11. class SecurityController extends AbstractController
  12. {
  13.     /**
  14.      * @var TranslatorInterface
  15.      */
  16.     protected $translator;
  17.     public function __construct(
  18.         protected Security $security,
  19.         TranslatorInterface $translator null,
  20.     ) {
  21.         $this->translator $translator;
  22.     }
  23.     /**
  24.      * @Route("/login", name="login")
  25.      */
  26.     public function login(AuthenticationUtils $authUtils)
  27.     {      
  28.         /*
  29.          * Redirect the user if he is currently logged in
  30.          */
  31.         
  32.         if($this->security->isGranted('IS_AUTHENTICATED_FULLY') || $this->security->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
  33.             return $this->redirectToRoute('home');
  34.         }
  35.       
  36.         /*
  37.          * Get the login error if there is one
  38.          */
  39.         $error $authUtils->getLastAuthenticationError();
  40.      
  41.         if($error) {
  42.             $this->addFlash('error'$this->translator->trans(
  43.                 $error->getMessageKey(),
  44.                 $error->getMessageData(),
  45.                 'security'
  46.             ));
  47.         }
  48.         /*
  49.          * Last username entered by the user
  50.          */
  51.         $lastUsername $authUtils->getLastUsername();
  52.        
  53.         return $this->render('application/login.html.twig', array(
  54.             'last_username' => $lastUsername,
  55.             'error'         => $error,
  56.         ));
  57.     }
  58. }