src/Controller/RegistrationController.php line 33

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