src/Controller/AuthController.php line 36

  1. <?php
  2. namespace App\Controller;
  3. use App\Controller\Bootstrap\DefaultLayoutController;
  4. use App\Entity\User;
  5. use App\Form\RegistrationFormType;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  10. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. class AuthController extends DefaultLayoutController
  13. {
  14.     public function signin(AuthenticationUtils $authenticationUtils): Response
  15.     {
  16.         if($redirectResponse $this->redirectIfLogged()) return $redirectResponse;
  17.         // get the login error if there is one
  18.         $error $authenticationUtils->getLastAuthenticationError();
  19.         $this->theme->addJavascriptFile('js/custom/authentication/sign-in/general.js');
  20.         // last username entered by the user
  21.         $lastUsername $authenticationUtils->getLastUsername();
  22.         return $this->render('pages/auth/signin.html.twig', [
  23.             'controller_name' => 'AuthController',
  24.             'last_username' => $lastUsername,
  25.             'error' => $error,
  26.             '_target_path' => $this->generateUrl('dashboard', []),
  27.         ]);
  28.     }
  29.     public function signup(Request $requestUserPasswordHasherInterface $userPasswordHasherEntityManagerInterface $entityManager): Response
  30.     {
  31.         if($redirectResponse $this->redirectIfLogged()) return $redirectResponse;
  32.         $this->theme->addJavascriptFile('js/custom/authentication/sign-up/general.js');
  33.         $user = new User();
  34.         $form $this->createForm(RegistrationFormType::class, $user);
  35.         $form->handleRequest($request);
  36.         if ($form->isSubmitted() && $form->isValid()) {
  37.             $user->setPassword(
  38.                 $userPasswordHasher->hashPassword(
  39.                     $user,
  40.                     $form->get('plainPassword')->getData()
  41.                 )
  42.             );
  43.             $entityManager->persist($user);
  44.             $entityManager->flush();
  45.             return $this->redirectToRoute('dashboard');
  46.         }
  47.         return $this->render('pages/auth/signup.html.twig', [
  48.             'registrationForm' => $form->createView(),
  49.         ]);
  50.     }
  51.     public function reset_password(): Response
  52.     {
  53.         if($redirectResponse $this->redirectIfLogged()) return $redirectResponse;
  54.         $this->theme->addJavascriptFile('js/custom/authentication/reset-password/reset-password.js');
  55.         return $this->render('pages/auth/reset-password.html.twig');
  56.     }
  57.     public function new_password(): Response
  58.     {
  59.         if($redirectResponse $this->redirectIfLogged()) return $redirectResponse;
  60.         $this->theme->addJavascriptFile('js/custom/authentication/reset-password/new-password.js');
  61.         return $this->render('pages/auth/new-password.html.twig');
  62.     }
  63.     protected function redirectIfLogged()
  64.     {
  65.         if($this->getUser()){
  66.             return new RedirectResponse($this->generateUrl('dashboard', []));
  67.         }
  68.         return null;
  69.     }
  70. }