src/Controller/RegistrationController.php line 74

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 $request, UserPasswordEncoderInterface $userPasswordEncoderInterface, GuardAuthenticatorHandler $guardHandler, LoginFormAuthenticator $authenticator, ValidatorInterface $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. $errors ="";
  37. if ($form->isSubmitted() && $form->isValid()) {
  38. // encode the plain password
  39. $user->setPassword(
  40. $userPasswordEncoderInterface->encodePassword(
  41. $user,
  42. $form->get('plainPassword')->getData()
  43. )
  44. );
  45. $entityManager = $this->getDoctrine()->getManager();
  46. $entityManager->persist($user);
  47. $entityManager->flush();
  48. $this->redirectToRoute('app_login');
  49. // generate a signed url and email it to the user
  50. /* $this->emailVerifier->sendEmailConfirmation(
  51. 'app_verify_email',
  52. $user,
  53. (new TemplatedEmail())
  54. ->from(new Address($this->getParameter('app_mail_from_address'), $this->getParameter('app_mail_from_name')))
  55. ->to($user->getEmail())
  56. ->subject('Please Confirm your Email')
  57. ->htmlTemplate('emails/registration/confirmation_email.html.twig')
  58. );
  59. // do anything else you need here, like send an email
  60. return $guardHandler->authenticateUserAndHandleSuccess(
  61. $user,
  62. $request,
  63. $authenticator,
  64. 'main' // firewall name in security.yaml
  65. );*/
  66. } else {
  67. $errors = $validator->validate($user);
  68. if (count($errors) == 0) {
  69. $errors = $validator->validate($user);
  70. }
  71. }
  72. return $this->render('registration/register.html.twig', [
  73. 'registrationForm' => $form->createView(),
  74. 'errors' => $errors,
  75. ]);
  76. }
  77. /**
  78. * @Route("/verify/email", name="app_verify_email")
  79. */
  80. public function verifyUserEmail(Request $request): Response
  81. {
  82. $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  83. // validate email confirmation link, sets User::isVerified=true and persists
  84. try {
  85. $this->emailVerifier->handleEmailConfirmation($request, $this->getUser());
  86. } catch (VerifyEmailExceptionInterface $exception) {
  87. $this->addFlash('verify_email_error', $exception->getReason());
  88. return $this->redirectToRoute('app_register');
  89. }
  90. $this->getUser()->setIsVerified(true);
  91. // @TODO Change the redirect on success and handle or remove the flash message in your templates
  92. $this->addFlash('success', 'Your email address has been verified.');
  93. return $this->redirectToRoute('app_account');
  94. }
  95. }