src/Controller/SecurityController.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Request;
  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.     /**
  11.      * @Route("/login", name="app_login")
  12.      */
  13.     public function login(AuthenticationUtils $authenticationUtils): Response
  14.     {
  15.         if ($this->getUser()) {
  16.              return $this->redirectToRoute('admin_index');
  17.         }
  18.         // get the login error if there is one
  19.         $error $authenticationUtils->getLastAuthenticationError();
  20.         // last username entered by the user
  21.         $lastUsername $authenticationUtils->getLastUsername();
  22.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  23.     }
  24.     /**
  25.      * @Route("/logout", name="app_logout")
  26.      */
  27.     public function logout()
  28.     {
  29.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  30.     }
  31.     /**
  32.      * @Route("/api/json_login", name="json_login", methods={"POST"})
  33.      */
  34.     public function json_login(Request $request): Response
  35.     {
  36.         $user $this->getUser();
  37.         return $this->json([
  38.             // The getUserIdentifier() method was introduced in Symfony 5.3.
  39.             // In previous versions it was called getUsername()
  40.             'username' => $user->getUserIdentifier(),
  41.             'roles' => $user->getRoles(),
  42.         ]);
  43.     }
  44. }