src/Controller/SecurityController.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Security\ForgotPasswordHandler;
  4. use LogicException;
  5. use Exception;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  11. use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
  12. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  13. class SecurityController extends AbstractController
  14. {
  15.     /**
  16.      * @Route("/login", name="app_login")
  17.      * @param AuthenticationUtils $authenticationUtils
  18.      * @return Response
  19.      */
  20.     public function login(AuthenticationUtils $authenticationUtils): Response
  21.     {
  22.         if ($this->getUser()) {
  23.             return $this->redirectToRoute('app_index');
  24.         }
  25.         $error $authenticationUtils->getLastAuthenticationError();
  26.         $lastUsername $authenticationUtils->getLastUsername();
  27.         return $this->render('login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  28.     }
  29.     /**
  30.      * @Route("/wachtwoord-vergeten", name="app_forgot_password")
  31.      * @param AuthenticationUtils $authenticationUtils
  32.      * @param ForgotPasswordHandler $handler
  33.      * @param Request $request
  34.      * @return Response
  35.      */
  36.     public function forgotPassword(
  37.         AuthenticationUtils $authenticationUtils,
  38.         ForgotPasswordHandler $handler,
  39.         Request $request
  40.     ): Response {
  41.         if ($this->getUser()) {
  42.             return $this->redirectToRoute('app_index');
  43.         }
  44.         $error null;
  45.         if($request->request->get('email') !== null) {
  46.             try {
  47.                 $handler->emailResetLink($request);
  48.                 return $this->redirectToRoute('app_index');
  49.             } catch (UsernameNotFoundException $exception) {
  50.                 $error $exception;
  51.             } catch (AuthenticationException $exception) {
  52.                 $error $exception;
  53.             }
  54.         }
  55.         $lastUsername $authenticationUtils->getLastUsername();
  56.         return $this->render('forgotPassword.html.twig', ['last_username' => $lastUsername'error' => $error]);
  57.     }
  58.     /**
  59.      * @Route("/logout", name="app_logout")
  60.      */
  61.     public function logout()
  62.     {
  63.         throw new LogicException(
  64.             'This method can be blank - it will be intercepted by the logout key on your firewall.'
  65.         );
  66.     }
  67. }