<?php
namespace App\Controller;
use Exception;
use JsonException;
use App\Form\EmailFormType;
use App\Form\RenewPasswordFormType;
use App\Repository\MenuRepository;
use App\Service\AuthService;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/password", priority="1", options={"expose"=true})
*/
class ResetPasswordController extends PageController
{
private AuthService $authService;
public function __construct(
ManagerRegistry $doctrine,
MenuRepository $menuRepository,
AuthService $authService
) {
parent::__construct($doctrine, $menuRepository);
$this->authService = $authService;
}
/**
* @Route("/recover", name="reset-password-recover")
*
* @throws Exception
*/
public function recover(Request $request): Response
{
$this->_initDatas($request, 'reset-password');
$form = $this->createForm(EmailFormType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$isSend = $this->authService->recover($form->get('email')->getData());
$dataResponse = ['recover_email_send' => $isSend];
} else {
$dataResponse = [
'requestForm' => $form->createView(),
];
}
return $this->render('reset_password/recover.html.twig', $this->getDatas($dataResponse));
}
public function verify(string $hash, Request $request): Response
{
$isValid = $this->authService->passwordVerify($hash);
if ($isValid) {
$email = base64_encode(urlencode($request->query->get('email')));
return $this->redirectToRoute('password-renew', ['hash' => $hash, md5('email') => $email]);
}
return $this->redirectToRoute('front_index_page');
}
/**
* @Route("/renew/{hash}", name="password-renew")
*
* @throws JsonException
*/
public function renew(string $hash, Request $request): Response
{
$data = [];
$dataResponse = null;
$email = urldecode(base64_decode($request->query->get(md5('email')), true));
$this->_initDatas($request, 'reset-password');
$form = $this->createForm(RenewPasswordFormType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$datas = $request->request->get('renew_password_form');
$data['password'] = $datas['password']['first'];
$data['email'] = $datas['email'];
$isResetted = $this->authService->resetPassword($hash, $data);
if ($isResetted) {
$this->addFlash('success', 'password.reset.success');
return $this->redirectToRoute('client_auth');
}
} else {
$dataResponse = [
'resetForm' => $form->createView(),
'email' => $email,
];
}
return $this->render('reset_password/reset.html.twig', $this->getDatas($dataResponse));
}
}