src/Controller/Front/SecurityController.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use Exception;
  4. use Throwable;
  5. use App\Adapter\AuthAdapter;
  6. use App\Controller\PageController;
  7. use App\Dto\Input\RegisterDTO;
  8. use App\Exception\InvalidFieldAuthenticationException;
  9. use App\Form\Registration\RegisterType;
  10. use App\Service\AppointmentService;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  16. /**
  17.  * @Route("/connexion")
  18.  */
  19. class SecurityController extends PageController
  20. {
  21.     use RequestRefererTrait;
  22.     /**
  23.      * @Route("/{rdv}", name="client_auth", options={"expose"=true}, defaults={"rdv"=""})
  24.      *
  25.      * @throws Exception
  26.      */
  27.     public function auth(AuthenticationUtils $authenticationUtilsRequest $requestAuthAdapter $adapter, ?string $rdv null): Response
  28.     {
  29.         $this->_initDatas($request'connexion');
  30.         if ($this->getUser() && $this->isGranted('ROLE_CLIENT')) {
  31.             return $this->redirectToRoute('front_index_page');
  32.         }
  33.         $this->setReferer($request);
  34.         $dataLogin $this->login($authenticationUtils);
  35.         $dataRegister $this->register($request$adapter);
  36.         if ($dataRegister instanceof RedirectResponse) {
  37.             return $dataRegister;
  38.         }
  39.         $data = [
  40.             'login' => $dataLogin,
  41.             'register' => $dataRegister,
  42.         ];
  43.         $data['rdv'] = $rdv;
  44.         if ($rdv) {
  45.             $data['show'] = true;
  46.         }
  47.         return $this->render('security/front/auth.html.twig'$this->getDatas($data));
  48.     }
  49.     protected function login(AuthenticationUtils $authenticationUtils): array
  50.     {
  51.         // get the login error if there is one
  52.         $error $authenticationUtils->getLastAuthenticationError();
  53.         $isValidField true;
  54.         if ($error instanceof InvalidFieldAuthenticationException) {
  55.             $isValidField false;
  56.         }
  57.         // last username entered by the user
  58.         $lastUsername $authenticationUtils->getLastUsername();
  59.         return ['last_username' => $lastUsername,
  60.             'error' => $error,
  61.             'valid_field' => $isValidField];
  62.     }
  63.     /**
  64.      * @return array|RedirectResponse
  65.      */
  66.     protected function register(Request $requestAuthAdapter $adapter)
  67.     {
  68.         $dto = new RegisterDTO();
  69.         $form $this->createForm(RegisterType::class, $dto);
  70.         $form->handleRequest($request);
  71.         if ($form->isSubmitted() && $form->isValid()) {
  72.             $dto->password $form->get('password')->getData();
  73.             $session $request->getSession();
  74.             try {
  75.                 $adapter->register($dto);
  76.                 $session->set('register_email'$dto->email);
  77.                 $session->set('register_password'$dto->password);
  78.                 if ($request->get('rdv') === AppointmentService::APPOINTMENT_SLUG) {
  79.                     $session->set('rdv'true);
  80.                 }
  81.                 return $this->redirectToRoute('registration_check_code');
  82.             } catch (Throwable $exception) {
  83.                 $this->addFlash(
  84.                     'error',
  85.                     $exception->getMessage()
  86.                 );
  87.             }
  88.         }
  89.         return [
  90.             'registerForm' => $form->createView(),
  91.         ];
  92.     }
  93. }