src/Controller/AuthController.php line 41

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. use App\Service\UserServiceInterface;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use App\Entity\User;
  10. use App\Form\PasswordRecoveryForm;
  11. use App\Form\PasswordUpdateForm;
  12. class AuthController extends AbstractController
  13. {
  14.   public function __construct(private UserServiceInterface $userService)
  15.   {}
  16.   // LOGIN
  17.   #[Route(path'/login'name'login'methods: ['POST''GET'])]
  18.   public function login(AuthenticationUtils $authenticationUtils): Response
  19.   {
  20.     // get the login error if there is one
  21.     $error $authenticationUtils->getLastAuthenticationError();
  22.     // last username entered by the user
  23.     $lastUsername $authenticationUtils->getLastUsername();
  24.     return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  25.   }
  26.   // LOGOUT
  27.   #[Route(path'/logout'name'logout'methods: ['GET'])]
  28.   public function logout(): void
  29.   {
  30.     throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  31.   }
  32.   // SEND PASSWORD RECOVERY
  33.   #[Route(path'/password/recovery/send'name'send_password_recovery'methods: ['POST''GET'])]
  34.   public function sendTokenRecovery(Request $request): Response
  35.   {
  36.     $user = new User();
  37.     $form $this->createForm(PasswordRecoveryForm::class, $user);
  38.     $form->handleRequest($request);
  39.     if($form->isSubmitted()){
  40.       $user $this->userService->findByEmail($user->getEmail());
  41.       if($user === null){
  42.         $this->addFlash('error'"L'email n'existe pas.");
  43.         return $this->render('security/passwordRecoveryForm.html.twig', [
  44.           'passwordRecoveryForm' => $form->createView()
  45.         ]);
  46.       }
  47.       $this->userService->sendPasswordRecoveryToken($user);
  48.       return $this->render('security/passwordRecoveryRequested.html.twig');
  49.     }
  50.     return $this->render('security/passwordRecoveryForm.html.twig', [
  51.       'passwordRecoveryForm' => $form->createView()
  52.     ]);
  53.   }
  54.   // PASSWORD RECOVERY
  55.   #[Route(path'/password/recovery/new/{token}'name'password_recovery'methods: ['POST''GET'])]
  56.   public function passwordRecovery(string $tokenRequest $request): Response
  57.   {
  58.     $user $this->userService->findByRecoveryToken($token);
  59.     if($user){
  60.       $form $this->createForm(PasswordUpdateForm::class, $user);
  61.       $form->handleRequest($request);
  62.       if($form->isSubmitted()){
  63.         $this->userService->updatePassword($user$form);
  64.         return $this->redirectToRoute('login');
  65.       }
  66.       return $this->render('security/passwordUpdateForm.html.twig', [
  67.         'passwordUpdateForm' => $form->createView()
  68.       ]);
  69.     }
  70.     return $this->redirectToRoute('home');
  71.   }
  72. }