src/Controller/ResetPasswordController.php line 25
<?php
namespace App\Controller;
use App\Entity\User;
use App\Event\SendVerificationEmailEvent;
use App\Form\ForgotPasswordEmailType;
use App\Form\ResetPasswordOtpType;
use App\Form\ResetPasswordType;
use App\Repository\UserRepository;
use App\Security\CorporateUserAuthenticator;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
#[Route('/app/password')]
class ResetPasswordController extends AbstractController
{
#[Route('/forgot', name: 'app_password_forgot')]
public function forgot(Request $request, EventDispatcherInterface $eventDispatcher, UserRepository $userRepository): Response
{
//@todo create a form to enter email id and generate otp and send to email id if account exists, then redirect to otp page
$form = $this->createForm(ForgotPasswordEmailType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$email = $form->get('email')->getData();
$session = $request->getSession();
$session->set('email', $email);
$user = $userRepository->findUserByEmail($email);
if ($user) {
$event = new SendVerificationEmailEvent($user, 'verifyOtp');
$eventDispatcher->dispatch($event, SendVerificationEmailEvent::NAME);
return $this->redirectToRoute('app_password_verify_otp');
}
}
return $this->render('reset_password/index.html.twig',[
'form' => $form
]);
}
#[Route('/otp/verify', name: 'app_password_verify_otp')]
public function verifyOtp(Request $request, UserRepository $userRepository): Response
{
//@todo show a form to verify otp and validate
$form = $this->createForm(ResetPasswordOtpType::class);
$form->handleRequest($request);
$email = $request->getSession()->get('email');
if ($form->isSubmitted() && $form->isValid()) {
$user = $userRepository->findUserByEmail($request->getSession()->get('email'));
$resetOtp = $form->get('otp')->getData();
if ($user->getResetPasswordOtp() == $resetOtp) {
return $this->redirectToRoute('app_password_reset');
} else {
return $this->redirectToRoute('app_password_verify_otp');
}
}
return $this->render('reset_password/verifyOtp.html.twig',[
'form' => $form,
'email' => $email
]);
}
#[Route('/reset', name: 'app_password_reset')]
public function reset(Request $request, UserPasswordHasherInterface $userPasswordHasher,
EntityManagerInterface $entityManager, UserRepository $userRepository): Response
{
//@todo show a form to reset password
$form = $this->createForm(ResetPasswordType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if ($form->get('password')->getData() == $form->get('confirmPassword')->getData()) {
$user = $userRepository->findUserByEmail($request->getSession()->get('email'));
$user->setPassword(
$userPasswordHasher->hashPassword($user, $form->get('password')->getData())
);
$entityManager->persist($user);
$entityManager->flush();
return $this->redirectToRoute('app_login');
}
}
return $this->render('reset_password/resetPassword.html.twig', [
'form' => $form
]);
}
#[Route('/otp/verify/resend', name: 'app_password_verify_otp_resend')]
public function resendVerifyOtp(Request $request, UserRepository $userRepository, EventDispatcherInterface $eventDispatcher) {
$user = $userRepository->findUserByEmail($request->getSession()->get('email'));
if ($user) {
$event = new SendVerificationEmailEvent($user, 'verifyOtp');
$eventDispatcher->dispatch($event, SendVerificationEmailEvent::NAME);
}
return $this->redirectToRoute('app_password_verify_otp');
}
}