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