Request resend of verification mail

This commit is contained in:
Frank
2026-01-14 13:09:32 +01:00
parent a90489da28
commit a6df6cbf0c
7 changed files with 103 additions and 3 deletions

View File

@@ -31,7 +31,7 @@ class ActivationController extends AbstractController
try {
$emailVerifier->handleEmailConfirmation($request, $user);
} catch (VerifyEmailExceptionInterface $exception) {
$this->addFlash('error', $exception->getReason());
$this->addFlash('error', $exception->getReason() . ' If the link has expired, you can <a href="' . $this->generateUrl('app_verify_resend_email') . '">request a new one here</a>.');
return $this->redirectToRoute('app_register');
}

View File

@@ -4,6 +4,8 @@ namespace App\Tech\Controller;
use App\Tech\Entity\User;
use App\Tech\Form\RegistrationFormType;
use App\Tech\Form\ResendVerificationEmailFormType;
use App\Tech\Repository\UserRepository;
use App\Tech\Service\EmailVerifier;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
@@ -54,4 +56,41 @@ class RegistrationController extends AbstractController
'registrationForm' => $form->createView(),
]);
}
#[Route('/verify/resend', name: 'app_verify_resend_email')]
public function resendVerificationEmail(Request $request, UserRepository $userRepository, EmailVerifier $emailVerifier): Response
{
$form = $this->createForm(ResendVerificationEmailFormType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$email = $form->get('email')->getData();
$user = $userRepository->findOneBy(['email' => $email]);
if ($user) {
if (!$user->isVerified()) {
$emailVerifier->sendEmailConfirmation('app_verify_email', $user,
(new TemplatedEmail())
->from($this->getParameter('mailer_from'))
->to($user->getEmail())
->subject('Please Confirm your Email')
->htmlTemplate('tech/registration/confirmation_email.html.twig')
);
} else {
$this->addFlash('info', 'This email address is already verified.');
return $this->redirectToRoute('app_login');
}
}
// We show the same success message regardless of whether the user exists or not,
// to avoid revealing whether an email is registered.
$this->addFlash('success', 'If an account exists with this email, a new confirmation link has been sent.');
return $this->redirectToRoute('website_home');
}
return $this->render('tech/registration/resend_verification_email.html.twig', [
'resendForm' => $form->createView(),
]);
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace App\Tech\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\NotBlank;
class ResendVerificationEmailFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('email', EmailType::class, [
'constraints' => [
new NotBlank([
'message' => 'Please enter your email',
]),
new Email([
'message' => 'Please enter a valid email address',
]),
],
])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([]);
}
}

View File

@@ -3,6 +3,7 @@
namespace App\Tech\Service;
use App\Tech\Entity\User;
use Symfony\Component\Security\Core\Exception\AccountStatusException;
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
use Symfony\Component\Security\Core\User\UserCheckerInterface;
use Symfony\Component\Security\Core\User\UserInterface;
@@ -16,7 +17,7 @@ class UserChecker implements UserCheckerInterface
}
if (!$user->isVerified()) {
throw new CustomUserMessageAuthenticationException('Your email address is not verified.');
throw new CustomUserMessageAuthenticationException('Your email address is not verified.', ['%resend_link%' => '/verify/resend']);
}
}