src/Controller/SecurityController.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Security\LoginFormAuthenticator;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\HttpFoundation\Session\Session;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
  11. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  12. use Symfony\Component\Security\Core\Security;
  13. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  14. use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
  15. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  16. class SecurityController extends AbstractController
  17. {
  18.     use TargetPathTrait;
  19.     /**
  20.      * @Route("/login", name="app_login")
  21.      * @param Request $request
  22.      * @param Security $security
  23.      * @param AuthenticationUtils $authenticationUtils
  24.      * @return Response
  25.      */
  26.     public function login(Request $requestSecurity $securityAuthenticationUtils $authenticationUtils): Response
  27.     {
  28.         // if user is already logged in, don't display the login page again
  29.         if ($security->isGranted('ROLE_USER')) {
  30.             return $this->redirectToRoute('Home');
  31.         }
  32.         // this statement solves an edge-case: if you change the locale in the login
  33.         // page, after a successful login you are redirected to a page in the previous
  34.         // locale. This code regenerates the referrer URL whenever the login page is
  35.         // browsed, to ensure that its locale is always the current one.
  36.         $this->saveTargetPath($request->getSession(), 'main'$this->generateUrl('Home'));
  37.         // get the login error if there is one
  38.         $error $authenticationUtils->getLastAuthenticationError();
  39.         // last username entered by the user
  40.         $lastUsername $authenticationUtils->getLastUsername();
  41.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  42.     }
  43.     /**
  44.      * @Route("/logout", name="app_logout")
  45.      */
  46.     public function logout()
  47.     {
  48.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  49.     }
  50. }