src/Security/SecurityAuthenticator.php line 31
<?php
namespace App\Security;
use App\Entity\User;
use App\Services\LogService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Csrf\CsrfToken;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
use Symfony\Component\Security\Http\Util\TargetPathTrait;
class SecurityAuthenticator extends AbstractAuthenticator
{
use TargetPathTrait;
public const LOGIN_ROUTE = 'app_login';
private $entityManager;
private $urlGenerator;
private $csrfTokenManager;
private $logService;
public function __construct(
LogService $logService,
EntityManagerInterface $entityManager,
UrlGeneratorInterface $urlGenerator,
CsrfTokenManagerInterface $csrfTokenManager
) {
$this->logService = $logService;
$this->entityManager = $entityManager;
$this->urlGenerator = $urlGenerator;
$this->csrfTokenManager = $csrfTokenManager;
}
public function supports(Request $request): bool
{
// Check if the request is a POST request to the login route
return $request->attributes->get('_route') === self::LOGIN_ROUTE
&& $request->isMethod('POST');
}
public function authenticate(Request $request): \Symfony\Component\Security\Http\Authenticator\Passport\Passport
{
// Retrieve credentials from the request
$credentials = [
'email' => $request->request->get('email'),
'password' => $request->request->get('password'),
'csrf_token' => $request->request->get('_csrf_token'),
];
// Store the last username in session for login page pre-fill
$request->getSession()->set(Security::LAST_USERNAME, $credentials['email']);
// Validate CSRF token
$token = new CsrfToken('authenticate', $credentials['csrf_token']);
if (!$this->csrfTokenManager->isTokenValid($token)) {
throw new InvalidCsrfTokenException();
}
// Find user by email
$user = $this->entityManager->getRepository(User::class)->findOneBy(['email' => $credentials['email']]);
if (!$user) {
throw new CustomUserMessageAuthenticationException('Email could not be found.');
}
// Return a Passport with user badge and password credentials
return new SelfValidatingPassport(
new UserBadge($credentials['email']),
[new PasswordCredentials($credentials['password'])]
);
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $providerKey): RedirectResponse
{
// Check for a target path after successful authentication
if ($targetPath = $this->getTargetPath($request->getSession(), $providerKey)) {
return new RedirectResponse($targetPath);
}
/** @var User $user */
$user = $token->getUser();
// Log the user login action
$this->logService->userLoggedIn($user);
// Redirect to the home page after successful login
return new RedirectResponse($this->urlGenerator->generate('app_home'));
}
public function onAuthenticationFailure(Request $request, AuthenticationException|\Symfony\Component\Security\Core\Exception\AuthenticationException $exception): RedirectResponse
{
// Redirect back to login page on authentication failure
return new RedirectResponse($this->urlGenerator->generate(self::LOGIN_ROUTE));
}
protected function getLoginUrl(): string
{
// Return the login page URL
return $this->urlGenerator->generate(self::LOGIN_ROUTE);
}
}