src/Security/SimpleAuthenticator.php line 21
<?phpnamespace App\Security;use Symfony\Component\HttpFoundation\RedirectResponse;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\Routing\Generator\UrlGeneratorInterface;use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;use Symfony\Component\Security\Core\Exception\AuthenticationException;use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;use Symfony\Component\Security\Http\Attribute\IsGranted;use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;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\Passport;use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;use Symfony\Component\Security\Http\Util\TargetPathTrait;#[IsGranted('IS_AUTHENTICATED_FULLY')]class SimpleAuthenticator extends AbstractAuthenticator implements AuthenticationEntryPointInterface{use TargetPathTrait;public const LOGIN_ROUTE = 'app_login';public function __construct(private UrlGeneratorInterface $urlGenerator,private string $adminUsername,private string $adminPassword) {}public function supports(Request $request): ?bool{return $request->isMethod('POST') && $request->getPathInfo() === '/login';}public function authenticate(Request $request): Passport{$username = $request->request->get('username', '');$password = $request->request->get('password', '');if (empty($username) || empty($password)) {throw new CustomUserMessageAuthenticationException('Please provide both username and password.');}return new Passport(new UserBadge($username, function ($username) {if ($username !== $this->adminUsername) {throw new CustomUserMessageAuthenticationException('Invalid credentials.');}return new SimpleUser($username);}),new CustomCredentials(function ($password) {return $password === $this->adminPassword;},$password));}public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response{if ($targetPath = $this->getTargetPath($request->getSession(), $firewallName)) {return new RedirectResponse($targetPath);}return new RedirectResponse($this->urlGenerator->generate('app_home'));}public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response{$request->getSession()->set('_security.last_error', $exception);return new RedirectResponse($this->urlGenerator->generate(self::LOGIN_ROUTE));}public function start(Request $request, AuthenticationException $authException = null): ?Response{return new RedirectResponse($this->urlGenerator->generate(self::LOGIN_ROUTE));}}