<?php
namespace App\Controller;
use App\Security\ForgotPasswordHandler;
use LogicException;
use Exception;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends AbstractController
{
/**
* @Route("/login", name="app_login")
* @param AuthenticationUtils $authenticationUtils
* @return Response
*/
public function login(AuthenticationUtils $authenticationUtils): Response
{
if ($this->getUser()) {
return $this->redirectToRoute('app_index');
}
$error = $authenticationUtils->getLastAuthenticationError();
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
}
/**
* @Route("/wachtwoord-vergeten", name="app_forgot_password")
* @param AuthenticationUtils $authenticationUtils
* @param ForgotPasswordHandler $handler
* @param Request $request
* @return Response
*/
public function forgotPassword(
AuthenticationUtils $authenticationUtils,
ForgotPasswordHandler $handler,
Request $request
): Response {
if ($this->getUser()) {
return $this->redirectToRoute('app_index');
}
$error = null;
if($request->request->get('email') !== null) {
try {
$handler->emailResetLink($request);
return $this->redirectToRoute('app_index');
} catch (UsernameNotFoundException $exception) {
$error = $exception;
} catch (AuthenticationException $exception) {
$error = $exception;
}
}
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('forgotPassword.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
}
/**
* @Route("/logout", name="app_logout")
*/
public function logout()
{
throw new LogicException(
'This method can be blank - it will be intercepted by the logout key on your firewall.'
);
}
}