src/Controller/Auth/SecurityController.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Auth;
  3. use App\Services\Api\Auth\AuthService;
  4. use App\Services\TokenService;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  10. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  11. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  12. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  13. class SecurityController extends AbstractController
  14. {
  15.     private AuthService $authService;
  16.     protected TokenService $tokenService;
  17.     public function __construct(AuthService $authServiceTokenService $tokenService)
  18.     {
  19.         $this->authService $authService;
  20.         $this->tokenService $tokenService;
  21.     }
  22.     /**
  23.      * @throws RedirectionExceptionInterface
  24.      * @throws ClientExceptionInterface
  25.      * @throws TransportExceptionInterface
  26.      * @throws ServerExceptionInterface
  27.      */
  28.     #[Route('/login'name'app_login')]
  29.     public function login(Request $requestRequestStack $requestStack)
  30.     {
  31.         if ($request->isMethod('POST')) {
  32.             $session $requestStack->getSession();
  33.             // Récupère les informations d'identification de l'utilisateur depuis le formulaire de connexion
  34.             $email $request->request->get('email');
  35.             $password $request->request->get('password');
  36.             //verification si les champ ne sont pas vide
  37.             if (!$email || !$password) {
  38.                 $this->addFlash('danger'"le mot de passe ou l'adresse électronique n'est pas correct");
  39.                 return $this->redirectToRoute('app_login');
  40.             }
  41.             $response =  $this->authService->createAuthenticatedClient([
  42.                 'email' => $email,
  43.                 'password' => $password
  44.             ]);
  45. //            dd($response->getContent());
  46.             if ($response->getStatusCode() == 200 || $response->getStatusCode() == 201) {
  47.                 // Authentification réussie
  48. //                return $this->redirectToRoute('app_home');
  49.             } else {
  50.                 // Authentification échouée
  51.                 $this->addFlash("danger"json_decode($response->getContent(false), true)['message']);
  52.                 return $this->redirectToRoute('app_login');
  53.             }
  54.             $data json_decode($response->getContent(), true);
  55.             // Stocke le jeton d'authentification dans la session utilisateur
  56.             $this->tokenService->setToken($data['token']);
  57.             $user $this->authService->getAllAboutMe();
  58.             $session->set('user'$user);
  59.             // Redirige l'utilisateur vers la page protégée
  60.             return $this->redirectToRoute('app_home');
  61.         }
  62.         return $this->render('frontend/security/login.html.twig', [
  63.         ]);
  64.     }
  65.     #[Route('/logout'name'app_logout')]
  66.     public function logout()
  67.     {
  68.         $this->tokenService->empty();
  69.         if (!$this->tokenService->isTokenValid()) {
  70.             return $this->redirectToRoute('app_login');
  71.         }
  72.     }
  73. }