createForm(ResetPasswordRequestFormType::class); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { /** @var string $email */ $email = $form->get('email')->getData(); return $this->processSendingPasswordResetEmail($email, $this->mailer, $this->translator); } return $this->render('reset_password/request.html.twig', [ 'requestForm' => $form, ]); } #[Route('/reset-password/check-email', name: 'tvdt_check_email')] public function checkEmail(): Response { if (!($resetToken = $this->getTokenObjectFromSession()) instanceof ResetPasswordToken) { $resetToken = $this->resetPasswordHelper->generateFakeResetToken(); } return $this->render('reset_password/check_email.html.twig', [ 'resetToken' => $resetToken, ]); } #[Route('/reset-password/reset/{token}', name: 'tvdt_reset_password')] public function reset(Request $request, ?string $token = null): Response { if ($token) { $this->storeTokenInSession($token); return $this->redirectToRoute('tvdt_reset_password'); } $token = $this->getTokenFromSession(); if (null === $token) { throw $this->createNotFoundException('No reset password token found in the URL or in the session.'); } try { /** @var User $user */ $user = $this->resetPasswordHelper->validateTokenAndFetchUser($token); } catch (ResetPasswordExceptionInterface $resetPasswordException) { $this->addFlash(FlashType::Danger->value, \sprintf( '%s - %s', $this->translator->trans(ResetPasswordExceptionInterface::MESSAGE_PROBLEM_VALIDATE, [], 'ResetPasswordBundle'), $this->translator->trans($resetPasswordException->getReason(), [], 'ResetPasswordBundle'), )); return $this->redirectToRoute('tvdt_forgot_password_request'); } $form = $this->createForm(ChangePasswordFormType::class); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $this->resetPasswordHelper->removeResetRequest($token); /** @var string $plainPassword */ $plainPassword = $form->get('plainPassword')->getData(); $user->password = $this->passwordHasher->hashPassword($user, $plainPassword); $this->entityManager->flush(); $this->cleanSessionAfterReset(); return $this->redirectToRoute('tvdt_backoffice_index'); } return $this->render('reset_password/reset.html.twig', [ 'resetForm' => $form, ]); } private function processSendingPasswordResetEmail(string $emailFormData, MailerInterface $mailer, TranslatorInterface $translator): RedirectResponse { $user = $this->entityManager->getRepository(User::class)->findOneBy([ 'email' => $emailFormData, ]); if (!$user instanceof User) { return $this->redirectToRoute('tvdt_check_email'); } try { $resetToken = $this->resetPasswordHelper->generateResetToken($user); } catch (ResetPasswordExceptionInterface) { return $this->redirectToRoute('tvdt_check_email'); } $email = new TemplatedEmail() ->to($user->getUserIdentifier()) ->subject($translator->trans('Your password reset request')) ->htmlTemplate('reset_password/email.html.twig') ->context([ 'resetToken' => $resetToken, ]); $mailer->send($email); $this->setTokenObjectInSession($resetToken); return $this->redirectToRoute('tvdt_check_email'); } }