src/Controller/SecurityController.php line 31

  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.     #[Route('/api/login'name'api_login'methods: ['POST'])]
  10.     public function index(): Response
  11.     {
  12.         $user $this->getUser();
  13.         
  14.         if (!$user) {
  15.             return $this->json([
  16.                 'error' => 'Non authentifié'
  17.             ], 401);
  18.         }
  19.         return $this->json([
  20.             'id' => $user->getId(),
  21.             'email' => $user->getUserIdentifier(),
  22.             'roles' => $user->getRoles()
  23.         ]);
  24.     }
  25.     #[Route(path'/login'name'app_login')]
  26.     public function login(AuthenticationUtils $authenticationUtils): Response
  27.     {
  28.         // if ($this->getUser()) {
  29.         //     return $this->redirectToRoute('target_path');
  30.         // }
  31.         // get the login error if there is one
  32.         $error $authenticationUtils->getLastAuthenticationError();
  33.         // last username entered by the user
  34.         $lastUsername $authenticationUtils->getLastUsername();
  35.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  36.     }
  37.     #[Route(path'/logout'name'app_logout')]
  38.     public function logout(): void
  39.     {
  40.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  41.     } 
  42. }