src/Controller/SecurityController.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  7. class SecurityController extends AbstractController
  8. {
  9.     //Controller action for handling user login.
  10.     #[Route(path'/auth-login'name'app_login')]
  11.     public function login(AuthenticationUtils $authenticationUtils): Response
  12.     {
  13.         // Redirect authenticated users to the home page
  14.         if ($this->getUser()) {
  15.             return $this->redirectToRoute('home');
  16.         }
  17.         // Retrieve authentication error, if any
  18.         $error $authenticationUtils->getLastAuthenticationError();
  19.         // Retrieve the last username entered by the user
  20.         $lastUsername $authenticationUtils->getLastUsername();
  21.         // Render the login template with relevant data
  22.         return $this->render('auth-login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  23.     }
  24.     //Controller action for handling user logout.
  25.     #[Route(path'/logout'name'app_logout')]
  26.     public function logout()
  27.     {
  28.         // Redirect to the login page after logout
  29.         return $this->redirectToRoute('app_login');
  30.     }
  31. }