src/Controller/RegistrationController.php line 29

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 Symfony\Bridge\Twig\Mime\TemplatedEmail;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Mime\Address;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  13. use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
  14. class RegistrationController extends AbstractController
  15. {
  16.     private $emailVerifier;
  17.     public function __construct(EmailVerifier $emailVerifier)
  18.     {
  19.         $this->emailVerifier $emailVerifier;
  20.     }
  21.     /**
  22.      * @Route("/register", name="app_register")
  23.      */
  24.     public function register(Request $requestUserPasswordHasherInterface $passwordEncoder): Response
  25.     {
  26.         $user = new User();
  27.         $user->setRoles(['ROLE_USER']);
  28.         $form $this->createForm(RegistrationFormType::class, $user);
  29.         $form->handleRequest($request);
  30.         if ($form->isSubmitted() && $form->isValid()) {
  31.             // encode the plain password
  32.             $user->setPassword(
  33.                 $passwordEncoder->hashPassword(
  34.                     $user,
  35.                     $form->get('plainPassword')->getData()
  36.                 )
  37.             );
  38.             $entityManager $this->getDoctrine()->getManager();
  39.             $entityManager->persist($user);
  40.             $entityManager->flush();
  41.             // generate a signed url and email it to the user
  42.             $this->emailVerifier->sendEmailConfirmation('app_verify_email'$user,
  43.                 (new TemplatedEmail())
  44.                     ->from(new Address('info@timelapse.app''Timelapse Mail Bot'))
  45.                     ->to($user->getEmail())
  46.                     ->subject('Please Confirm your Email')
  47.                     ->htmlTemplate('registration/confirmation_email.html.twig')
  48.             );
  49.             // do anything else you need here, like send an email
  50.             return $this->redirectToRoute('user');
  51.         }
  52.         return $this->render('registration/register.html.twig', [
  53.             'registrationForm' => $form->createView(),
  54.         ]);
  55.     }
  56.     /**
  57.      * @Route("/verify/email", name="app_verify_email")
  58.      */
  59.     public function verifyUserEmail(Request $request): Response
  60.     {
  61.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  62.         // validate email confirmation link, sets User::isVerified=true and persists
  63.         try {
  64.             $this->emailVerifier->handleEmailConfirmation($request$this->getUser());
  65.         } catch (VerifyEmailExceptionInterface $exception) {
  66.             $this->addFlash('verify_email_error'$exception->getReason());
  67.             return $this->redirectToRoute('app_register');
  68.         }
  69.         // @TODO Change the redirect on success and handle or remove the flash message in your templates
  70.         $this->addFlash('success''Your email address has been verified.');
  71.         return $this->redirectToRoute('app_register');
  72.     }
  73. }