src/Controller/Security/SecurityController.php line 18

Open in your IDE?
  1. <?php
  2. // src/Controller/SecurityController.php
  3. namespace App\Controller\Security;
  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\Authentication\Token\UsernamePasswordToken;
  9. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  10. use App\Repository\ClientRepository;
  11. class SecurityController extends AbstractController
  12. {
  13.     #[Route('/login'name'login')]
  14.     public function login(AuthenticationUtils $authenticationUtils): \Symfony\Component\HttpFoundation\Response
  15.     {
  16.         $error $authenticationUtils->getLastAuthenticationError();
  17.         $lastUsername $authenticationUtils->getLastUsername();
  18.         $user $this->getUser();
  19.         if(!empty($user)){
  20.             return $this->redirectToRoute('client_espace');
  21.             die;
  22.         }
  23.         return $this->render('security/login.html.twig', [
  24.             'last_username' => $lastUsername,
  25.             'error' => $error,
  26.             'path_name' => 'login'
  27.         ]);
  28.     }
  29.     #[Route('/force-login/{id}'name'force_login')]
  30.     public function forceLogin(int $idTokenStorageInterface $tokenStorageClientRepository $clientRepository)
  31.     {
  32.         $client $clientRepository->find($id);
  33.         if (!$client) {
  34.             return $this->viewHandler->handle(View::create(['message' => 'Client not found'], Response::HTTP_NOT_FOUND));
  35.         }
  36.         $token = new UsernamePasswordToken($client'website'$client->getRoles());
  37.         $tokenStorage->setToken($token);
  38.         $user $this->getUser();
  39.         return $this->redirectToRoute('client_espace');
  40.     }
  41.     #[Route('/logout'name'logout')]
  42.     public function logout(): void
  43.     {
  44.         // Symfony gère automatiquement la déconnexion
  45.     }
  46. }