<?php
namespace App\Controller;
use App\Service\LoginAttemptService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends AbstractController
{
private $loginAttemptService;
public function __construct(LoginAttemptService $loginAttemptService)
{
$this->loginAttemptService = $loginAttemptService;
}
/**
* @Route("/login", name="app_login")
*/
public function login(AuthenticationUtils $authenticationUtils): Response
{
if ($this->getUser()) {
return $this->redirectToRoute('app_dashboard');
}
$error = $authenticationUtils->getLastAuthenticationError();
$lastUsername = $authenticationUtils->getLastUsername();
$errorMessage = null;
$remainingAttempts = null;
if ($error) {
$errorMessage = 'Email ou mot de passe incorrect.';
if ($this->getUser()) {
$user = $this->getUser();
$remainingAttempts = $this->loginAttemptService->getRemainingAttempts($user);
if ($remainingAttempts > 0) {
$errorMessage .= sprintf(' Il vous reste %d tentative(s).', $remainingAttempts);
}
}
}
return $this->render('security/login.html.twig', [
'last_username' => $lastUsername,
'error' => $errorMessage,
'remaining_attempts' => $remainingAttempts,
]);
}
/**
* @Route("/logout", name="app_logout")
*/
public function logout(): void
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
}