src/Controller/RegistrationController.php line 78

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\RegistrationFormType;
  5. use App\Security\EmailVerifier;
  6. use App\Security\LoginFormAuthenticator;
  7. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Mime\Address;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  14. use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
  15. use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
  16. use Symfony\Component\Validator\Validator\ValidatorInterface;
  17. class RegistrationController extends AbstractController
  18. {
  19.     private EmailVerifier $emailVerifier;
  20.     public function __construct(EmailVerifier $emailVerifier)
  21.     {
  22.         $this->emailVerifier $emailVerifier;
  23.     }
  24.     /**
  25.      * @Route("/register", name="app_register")
  26.      */
  27.     public function register(Request $requestUserPasswordEncoderInterface $userPasswordEncoderInterfaceGuardAuthenticatorHandler $guardHandlerLoginFormAuthenticator $authenticatorValidatorInterface $validator): Response
  28.     {
  29.         if ($this->getUser()) {
  30.             $this->addFlash('warning''Already exist!');
  31.             return $this->redirectToRoute('app_account');
  32.         }
  33.         $user = new User();
  34.         $form $this->createForm(RegistrationFormType::class, $user);
  35.         $form->handleRequest($request);
  36.         if ($form->isSubmitted() && $form->isValid()) {
  37.             // encode the plain password
  38.             $user->setPassword(
  39.                 $userPasswordEncoderInterface->encodePassword(
  40.                     $user,
  41.                     $form->get('plainPassword')->getData()
  42.                 )
  43.             );
  44.             $entityManager $this->getDoctrine()->getManager();
  45.             $entityManager->persist($user);
  46.             $entityManager->flush();
  47.             // generate a signed url and email it to the user
  48.            $this->emailVerifier->sendEmailConfirmation(
  49.                 'app_verify_email',
  50.                 $user,
  51.                 (new TemplatedEmail())
  52.                     ->from(new Address($this->getParameter('app_mail_from_address'), $this->getParameter('app_mail_from_name')))
  53.                     ->to($user->getEmail())
  54.                     ->subject('Please Confirm your Email')
  55.                     ->htmlTemplate('emails/registration/confirmation_email.html.twig')
  56.             ); 
  57.             // do anything else you need here, like send an email
  58.             return $guardHandler->authenticateUserAndHandleSuccess(
  59.                 $user,
  60.                 $request,
  61.                 $authenticator,
  62.                 'main' // firewall name in security.yaml
  63.             );
  64.         } else {
  65.             $errors $validator->validate($user);
  66.             if (count($errors) == 0) {
  67.                 $errors $validator->validate($user);
  68.             }
  69.         }
  70.         return $this->render('registration/register.html.twig', [
  71.             'registrationForm' => $form->createView(),
  72.             'errors' => $errors,
  73.         ]);
  74.     }
  75.     /**
  76.      * @Route("/verify/email", name="app_verify_email")
  77.      */
  78.     public function verifyUserEmail(Request $request): Response
  79.     {
  80.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  81.         // validate email confirmation link, sets User::isVerified=true and persists
  82.         try {
  83.             $this->emailVerifier->handleEmailConfirmation($request$this->getUser());
  84.         } catch (VerifyEmailExceptionInterface $exception) {
  85.             $this->addFlash('verify_email_error'$exception->getReason());
  86.             return $this->redirectToRoute('app_register');
  87.         }
  88.         $this->getUser()->setIsVerified(true);
  89.         // @TODO Change the redirect on success and handle or remove the flash message in your templates
  90.         $this->addFlash('success''Your email address has been verified.');
  91.         return $this->redirectToRoute('app_account');
  92.     }
  93. }