src/Controller/SecurityController.php line 20

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\CreateAccountType;
  5. use App\Service\Mail\MailService;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  13. class SecurityController extends AbstractController
  14. {
  15.     #[Route(path'/login'name'login')]
  16.     public function login(AuthenticationUtils $authenticationUtils): Response
  17.     {
  18.         if ($this->getUser()) {
  19.             return $this->redirectToRoute('home');
  20.         }
  21.         // get the login error if there is one
  22.         $error $authenticationUtils->getLastAuthenticationError();
  23.         // last username entered by the user
  24.         $lastUsername $authenticationUtils->getLastUsername();
  25.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  26.     }
  27.     #[Route('/logout'name'logout')]
  28.     public function logout(): void
  29.     {
  30.     }
  31.     #[Route('/create_account'name'create_account')]
  32.     public function createAccount(Request $requestUserPasswordHasherInterface $passwordHasherEntityManagerInterface $managerMailService $mailService): Response
  33.     {
  34.         $user = new User();
  35.         $form $this->createForm(CreateAccountType::class, $user);
  36.         $form->handleRequest($request);
  37.         if($form->isSubmitted() && $form->isValid()) {
  38.             $password $passwordHasher->hashPassword($user$user->getPassword());
  39.             $user->setPassword($password);
  40.             $manager->persist($user);
  41.             $manager->flush();
  42.             $mailService->sendMailAccountCreation($user->getUserEmail(), $user->getUserName(), $user->getUserSurName());
  43.             return $this->redirectToRoute('login');
  44.         }
  45.         return $this->render('security/create_account.html.twig', [
  46.             'controller_name' => 'SecurityController',
  47.             'form' => $form->createView(),
  48.         ]);
  49.     }
  50. }