src/Controller/RegistrationController.php line 35

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\RegistrationFormType;
  5. use App\Repository\UserRepository;
  6. use App\Security\EmailVerifier;
  7. use App\Security\HappyAuthenticator;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Mime\Address;
  14. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  17. use Symfony\Contracts\Translation\TranslatorInterface;
  18. use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
  19. class RegistrationController extends AbstractController
  20. {
  21. private EmailVerifier $emailVerifier;
  22. public function __construct(EmailVerifier $emailVerifier)
  23. {
  24. $this->emailVerifier = $emailVerifier;
  25. }
  26. #[Route('/register', name: 'app_register')]
  27. public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, UserAuthenticatorInterface $userAuthenticator, HappyAuthenticator $authenticator, EntityManagerInterface $entityManager): Response
  28. {
  29. $user = new User();
  30. $form = $this->createForm(RegistrationFormType::class, $user);
  31. $form->handleRequest($request);
  32. if ($form->isSubmitted() && $form->isValid()) {
  33. // encode the plain password
  34. $user->setRoles(['ROLE_USER']);
  35. $user->setPassword(
  36. $userPasswordHasher->hashPassword(
  37. $user,
  38. $form->get('plainPassword')->getData()
  39. )
  40. );
  41. $entityManager->persist($user);
  42. $entityManager->flush();
  43. // generate a signed url and email it to the user
  44. // $this->emailVerifier->sendEmailConfirmation('app_verify_email', $user,
  45. // (new TemplatedEmail())
  46. // ->from(new Address('no-replay@happylauncher.com', 'Happy Launcher'))
  47. // ->to($user->getEmail())
  48. // ->subject('Please Confirm your Email')
  49. // ->htmlTemplate('registration/confirmation_email.html.twig')
  50. // );
  51. $this->addFlash('success', 'Your account has been created.');
  52. // do anything else you need here, like send an email
  53. return $userAuthenticator->authenticateUser(
  54. $user,
  55. $authenticator,
  56. $request
  57. );
  58. }
  59. return $this->render('registration/register.html.twig', [
  60. 'registrationForm' => $form->createView(),
  61. ]);
  62. }
  63. #[Route('/verify/email', name: 'app_verify_email')]
  64. public function verifyUserEmail(Request $request, TranslatorInterface $translator, UserRepository $userRepository): Response
  65. {
  66. $id = $request->get('id');
  67. if (null === $id) {
  68. return $this->redirectToRoute('app_register');
  69. }
  70. $user = $userRepository->find($id);
  71. if (null === $user) {
  72. return $this->redirectToRoute('app_register');
  73. }
  74. // validate email confirmation link, sets User::isVerified=true and persists
  75. try {
  76. $this->emailVerifier->handleEmailConfirmation($request, $user);
  77. } catch (VerifyEmailExceptionInterface $exception) {
  78. $this->addFlash('verify_email_error', $translator->trans($exception->getReason(), [], 'VerifyEmailBundle'));
  79. return $this->redirectToRoute('app_register');
  80. }
  81. // @TODO Change the redirect on success and handle or remove the flash message in your templates
  82. $this->addFlash('success', 'Your email address has been verified.');
  83. return $this->redirectToRoute('app_register');
  84. }
  85. }