src/Controller/AuthController.php line 39

Open in your IDE?
  1. <?php 
  2. namespace App\Controller;
  3. use App\Entity\Customer;
  4. use App\Event\SignupEvent;
  5. use App\Form\SignupType;
  6. use App\Security\EmailVerifier;
  7. use Boab\CmsBundle\Controller\BaseController;
  8. use Boab\CmsBundle\Repository\UserRepositoryInterface;
  9. use Boab\CmsBundle\View\ViewManagerInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
  15. class AuthController extends BaseController
  16. {
  17.     private EmailVerifier $emailVerifier;
  18.     public function __construct(EmailVerifier $emailVerifier)
  19.     {
  20.         $this->emailVerifier $emailVerifier;
  21.     }
  22.     public function login(Request $requestAuthenticationUtils $authenticationUtils)
  23.     {
  24.         return $this->render('auth/login.html.twig',[
  25.             "authError" => $authenticationUtils->getLastAuthenticationError(),
  26.             "pageTitle" => 'Login'
  27.         ]);
  28.     }
  29.     public function loginCheck(Request $request){}    
  30.     public function signUp(Request $requestViewManagerInterface $viewstring $_route)
  31.     {
  32.         $form $this->createForm(SignupType::class, new Customer,[
  33.             "action" => $this->router->generate($_route),
  34.             "method"=>'post'
  35.         ]);
  36.         $form->handleRequest($request);
  37.         if($form->isSubmitted() && $form->isValid()){
  38.             $customer $form->getData();
  39.             // encode the plain password
  40.             $this->save($customer);
  41.             $event = new SignupEvent();
  42.             $event->setCustomer($customer);
  43.             $this->eventDispatcher->dispatch($event'customer.sign_up');
  44.             
  45.             return $this->redirectToRoute('app.login');
  46.         }
  47.         $view $view->load('auth/sign_up.html.twig');
  48.         $view->form $form->createView();
  49.         $view->pageTitle 'Sign up';
  50.         return $view
  51.     } 
  52.     /**
  53.      * @Route("/verify/email", name="app.verify_email")
  54.      */
  55.     public function verifyUserEmail(Request $requestUserRepositoryInterface $studentRepository): Response
  56.     {
  57.         $id $request->get('id');
  58.         if (null === $id) {
  59.             return $this->redirectToRoute('app.sign_up');
  60.         }
  61.         $user $studentRepository->findOneBy(['id'=>$id]);
  62.         if (null === $user) {
  63.             return $this->redirectToRoute('app.sign_up');
  64.         }
  65.         // validate email confirmation link, sets User::isVerified=true and persists
  66.         try {
  67.             $this->emailVerifier->handleEmailConfirmation($request$user);
  68.         } catch (VerifyEmailExceptionInterface $exception) {
  69.             $this->flash->setWarning($exception->getReason());
  70.             return $this->redirectToRoute('app.sign_up');
  71.         }
  72.         // @TODO Change the redirect on success and handle or remove the flash message in your templates
  73.         $this->flash->setSuccess('Your email address has been verified.');
  74.         return $this->redirectToRoute('app.sign_up');
  75.     }    
  76. }