src/Controller/SecurityController.php line 21

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\Entity\User;
  8. use App\Form\RegisterFormType;
  9. use App\Security\LoginFormAuthenticator;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  12. use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
  13. class SecurityController extends AbstractController
  14. {
  15.     /**
  16.      * @Route("/", name="app_login")
  17.      */
  18.     public function login(AuthenticationUtils $authenticationUtils): Response
  19.     {
  20.         if ($this->getUser()) {
  21.             return $this->redirectToRoute('home');
  22.         }
  23.         // get the login error if there is one
  24.         $error $authenticationUtils->getLastAuthenticationError();
  25.         // last username entered by the user
  26.         $lastUsername $authenticationUtils->getLastUsername();
  27.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  28.     }
  29.     /**
  30.      * @Route("/logout", name="app_logout")
  31.      */
  32.     public function logout()
  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="register")
  38.      */
  39.     public function register(Request $requestUserPasswordEncoderInterface $passwordEncoderGuardAuthenticatorHandler $guardHandlerLoginFormAuthenticator $authenticator)
  40.     {
  41.     $user = new User();
  42.         $form $this->createForm(RegisterFormType::class, $user);
  43.         $form->handleRequest($request);
  44.         if ($form->isSubmitted() && $form->isValid()) {
  45.             // encode the plain password
  46.             $user->setPassword(
  47.                 $passwordEncoder->encodePassword(
  48.                     $user,
  49.                     $form->get('password')->getData()
  50.                 )
  51.             );
  52.             $entityManager $this->getDoctrine()->getManager();
  53.             $entityManager->persist($user);
  54.             $entityManager->flush();
  55.             // do anything else you need here, like send an email
  56.             return $guardHandler->authenticateUserAndHandleSuccess(
  57.                 $user,
  58.                 $request,
  59.                 $authenticator,
  60.                 'main' // firewall name in security.yaml
  61.             );
  62.         }
  63.         return $this->render('security/register.html.twig', [
  64.             'form' => $form->createView(),
  65.         ]);
  66.     }
  67. }