src/Controller/SecurityController.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\RegistrationFormType;
  5. use Doctrine\ORM\EntityManagerInterface;
  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 Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  12. class SecurityController extends AbstractController
  13. {
  14.     /**
  15.      * @Route("/login", name="app_login")
  16.      */
  17.     public function login(AuthenticationUtils $authenticationUtils): Response
  18.     {
  19.         if ($this->getUser()) {
  20.             return $this->redirectToRoute('instance_index');
  21.         }
  22.         $error $authenticationUtils->getLastAuthenticationError();
  23.         $lastUsername $authenticationUtils->getLastUsername();
  24.         return $this->render('security/login.html.twig', [
  25.             'last_username' => $lastUsername,
  26.             'error' => $error,
  27.         ]);
  28.     }
  29.     /**
  30.      * @Route("/logout", name="app_logout")
  31.      */
  32.     public function logout(): void
  33.     {
  34.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  35.     }
  36.     /**
  37.      * @Route("/register", name="app_register")
  38.      */
  39.     public function register(Request $requestUserPasswordHasherInterface $passwordHasherEntityManagerInterface $entityManager): Response
  40.     {
  41.         if ($this->getUser()) {
  42.             return $this->redirectToRoute('instance_index');
  43.         }
  44.         $user = new User();
  45.         $form $this->createForm(RegistrationFormType::class, $user);
  46.         $form->handleRequest($request);
  47.         if ($form->isSubmitted() && $form->isValid()) {
  48.             $plainPassword $form->get('plainPassword')->getData();
  49.             $hashedPassword $passwordHasher->hashPassword($user$plainPassword);
  50.             $user->setPassword($hashedPassword);
  51.             $entityManager->persist($user);
  52.             $entityManager->flush();
  53.             return $this->redirectToRoute('app_login');
  54.         }
  55.         return $this->render('security/register.html.twig', [
  56.             'registrationForm' => $form->createView(),
  57.         ]);
  58.     }
  59. }