src/Controller/Security/SecurityController.php line 58

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Security;
  3. use App\Entity\Security\User;
  4. use App\Form\Security\RegistrationType;
  5. use App\Security\Security\UserManager;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  12. class SecurityController extends AbstractController
  13. {
  14.     public function __construct(
  15.         private readonly UserManager $userManager,
  16.     ) {
  17.     }
  18.     #[Route(path'/login'name'security_login')]
  19.     public function login(AuthenticationUtils $authenticationUtils): Response
  20.     {
  21.         // if ($this->getUser()) {
  22.         //     return $this->redirectToRoute('target_path');
  23.         // }
  24.         // get the login error if there is one
  25.         $error $authenticationUtils->getLastAuthenticationError();
  26.         // last username entered by the user
  27.         $lastUsername $authenticationUtils->getLastUsername();
  28.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  29.     }
  30.     #[Route(path'/logout'name'security_logout')]
  31.     public function logout(): void
  32.     {
  33.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  34.     }
  35.     #[Route(path'/register'name'security_register')]
  36.     public function register(Request $request): RedirectResponse|Response
  37.     {
  38.         $user = new User();
  39.         $registrationType $this->createForm(RegistrationType::class, $user);
  40.         $registrationType->handleRequest($request);
  41.         if ($registrationType->isSubmitted() && $registrationType->isValid()) {
  42.             $this->userManager->save($user);
  43.             $this->addFlash('success''Inscription réalisée avec succès, vous pouvez vous connecter');
  44.             return $this->redirectToRoute('security_login');
  45.         }
  46.         return $this->render('security/register.html.twig', [
  47.             'registrationType' => $registrationType->createView(),
  48.         ]);
  49.     }
  50. }