src/Security/LoginFormAuthenticator.php line 23
<?phpnamespace App\Security;use App\Entity\User;use App\Repository\UserRepository;use Symfony\Component\HttpFoundation\RedirectResponse;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\Routing\Generator\UrlGeneratorInterface;use Symfony\Component\Routing\RouterInterface;use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;use Symfony\Component\Security\Core\Exception\AuthenticationException;use Symfony\Component\Security\Core\Exception\UserNotFoundException;use Symfony\Component\Security\Core\Security;use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\CustomCredentials;use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;use Symfony\Component\Security\Http\Authenticator\Passport\Passport;use Symfony\Component\Security\Http\Util\TargetPathTrait;class LoginFormAuthenticator extends AbstractLoginFormAuthenticator{use TargetPathTrait;public const LOGIN_ROUTE = 'app_login';public function __construct(private readonly UrlGeneratorInterface $urlGenerator,private readonly UserRepository $userRepository,private readonly RouterInterface $router){}public function authenticate(Request $request): Passport{$email = $request->request->get('email', '');$password = $request->request->get('password', '');//$request->getSession()->set(Security::LAST_USERNAME, $email);/*return new Passport(new UserBadge($email),new PasswordCredentials($password),[new CsrfTokenBadge('authenticate', $request->request->get('_csrf_token')),]);*/return new Passport(new UserBadge($email, function($userIdentifier) {// Optionally pass a callback to load the User manually$user = $this->userRepository->findOneBy(['email' => $userIdentifier]);if (!$user) {throw new UserNotFoundException();}return $user;}),new PasswordCredentials($password));}public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response{if ($targetPath = $this->getTargetPath($request->getSession(), $firewallName)) {return new RedirectResponse($targetPath);}// For example:return new RedirectResponse($this->urlGenerator->generate('app_index'));}public function onAuthenticationFailure(Request $request, AuthenticationException $exception): Response{$request->getSession()->set(Security::AUTHENTICATION_ERROR, $exception);return new RedirectResponse($this->router->generate('app_login'));}protected function getLoginUrl(Request $request): string{return $this->urlGenerator->generate(self::LOGIN_ROUTE);}}