src/Controller/RegistrationController.php line 30
<?phpnamespace App\Controller;use App\Entity\User;use App\Form\Builder\RegistrationFormType;use App\Security\EmailVerifier;use Doctrine\ORM\EntityManagerInterface;use Symfony\Bridge\Twig\Mime\TemplatedEmail;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\Mime\Address;use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;use Symfony\Component\Routing\Annotation\Route;use Symfony\Contracts\Translation\TranslatorInterface;use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;class RegistrationController extends AbstractController{private EmailVerifier $emailVerifier;public function __construct(EmailVerifier $emailVerifier){$this->emailVerifier = $emailVerifier;}#[Route('/register', name: 'app_register')]public function register(Request $request,UserPasswordHasherInterface $userPasswordHasher,EntityManagerInterface $entityManager): Response {if ($this->getUser()) {return $this->redirectToRoute('page_index');}$user = new User();$form = $this->createForm(RegistrationFormType::class, $user);$form->handleRequest($request);if ($form->isSubmitted() && $form->isValid()) {$user->setPassword($userPasswordHasher->hashPassword($user,$form->get('plainPassword')->getData()));$entityManager->persist($user);$entityManager->flush();$this->emailVerifier->sendEmailConfirmation('app_verify_email',$user,(new TemplatedEmail())->from(new Address('example@example.com', 'Vrshikyans'))->to($user->getEmail())->subject('Please Confirm your Email')->htmlTemplate('registration/confirmation_email.html.twig'));return $this->redirectToRoute('sonata_admin_dashboard');}return $this->render('registration/register.html.twig', ['registrationForm' => $form->createView(),]);}#[Route('/auth_register', name: 'app_auth_register', methods: ['POST'])]public function authRegister(Request $request,UserPasswordHasherInterface $userPasswordHasher,EntityManagerInterface $entityManager): \Symfony\Component\HttpFoundation\JsonResponse{// Only accept JSON$data = json_decode($request->getContent(), true);if (!$data) {return $this->json(['message' => 'Invalid JSON'], 400);}$username = $data['username'] ?? null;$email = $data['email'] ?? null;$password = $data['password'] ?? null;$errors = [];// Validate usernameif (!$username || strlen($username) < 3) {$errors['username'] = 'Username should be at least 3 characters.';}// Validate emailif (!$email || !filter_var($email, FILTER_VALIDATE_EMAIL)) {$errors['email'] = 'Enter a valid email address.';}// Validate passwordif (!$password || strlen($password) < 8) {$errors['password'] = 'Password should be at least 8 characters.';}if ($errors) {return $this->json(['message' => 'Validation failed', 'errors' => $errors], 400);}// Create user$user = new User();$user->setUsername($username);$user->setEmail($email);$user->setPassword($userPasswordHasher->hashPassword($user, $password));$entityManager->persist($user);$entityManager->flush();// Send email verification$this->emailVerifier->sendEmailConfirmation('app_verify_email',$user,(new TemplatedEmail())->from(new Address('example@example.com', 'Vrshikyans'))->to($user->getEmail())->subject('Please Confirm your Email')->htmlTemplate('registration/confirmation_email.html.twig'));return $this->json(['message' => 'Account created successfully. Please check your email to verify.'], 200);}#[Route('/verify/email', name: 'app_verify_email')]public function verifyUserEmail(Request $request, TranslatorInterface $translator): Response{$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');// validate email confirmation link, sets User::isVerified=true and persiststry {$this->emailVerifier->handleEmailConfirmation($request, $this->getUser());} catch (VerifyEmailExceptionInterface $exception) {$this->addFlash('verify_email_error', $translator->trans($exception->getReason(), [], 'VerifyEmailBundle'));return $this->redirectToRoute('app_register');}// @TODO Change the redirect on success and handle or remove the flash message in your templates$this->addFlash('success', 'Your email address has been verified.');return $this->redirectToRoute('app_register');}}