<?php
namespace App\Controller;
use App\Entity\Customer;
use App\Event\SignupEvent;
use App\Form\SignupType;
use App\Security\EmailVerifier;
use Boab\CmsBundle\Controller\BaseController;
use Boab\CmsBundle\Repository\UserRepositoryInterface;
use Boab\CmsBundle\View\ViewManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Routing\Annotation\Route;
use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
class AuthController extends BaseController
{
private EmailVerifier $emailVerifier;
public function __construct(EmailVerifier $emailVerifier)
{
$this->emailVerifier = $emailVerifier;
}
public function login(Request $request, AuthenticationUtils $authenticationUtils)
{
return $this->render('auth/login.html.twig',[
"authError" => $authenticationUtils->getLastAuthenticationError(),
"pageTitle" => 'Login'
]);
}
public function loginCheck(Request $request){}
public function signUp(Request $request, ViewManagerInterface $view, string $_route)
{
$form = $this->createForm(SignupType::class, new Customer,[
"action" => $this->router->generate($_route),
"method"=>'post'
]);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()){
$customer = $form->getData();
// encode the plain password
$this->save($customer);
$event = new SignupEvent();
$event->setCustomer($customer);
$this->eventDispatcher->dispatch($event, 'customer.sign_up');
return $this->redirectToRoute('app.login');
}
$view = $view->load('auth/sign_up.html.twig');
$view->form = $form->createView();
$view->pageTitle = 'Sign up';
return $view;
}
/**
* @Route("/verify/email", name="app.verify_email")
*/
public function verifyUserEmail(Request $request, UserRepositoryInterface $studentRepository): Response
{
$id = $request->get('id');
if (null === $id) {
return $this->redirectToRoute('app.sign_up');
}
$user = $studentRepository->findOneBy(['id'=>$id]);
if (null === $user) {
return $this->redirectToRoute('app.sign_up');
}
// validate email confirmation link, sets User::isVerified=true and persists
try {
$this->emailVerifier->handleEmailConfirmation($request, $user);
} catch (VerifyEmailExceptionInterface $exception) {
$this->flash->setWarning($exception->getReason());
return $this->redirectToRoute('app.sign_up');
}
// @TODO Change the redirect on success and handle or remove the flash message in your templates
$this->flash->setSuccess('Your email address has been verified.');
return $this->redirectToRoute('app.sign_up');
}
}