src/Security/LoginAuthenticator.php line 41
<?phpnamespace App\Security;use App\Entity\User;use Exception;use GuzzleHttp\Client;use GuzzleHttp\Exception\GuzzleException;use Psr\Log\LoggerInterface;use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;use Symfony\Component\HttpFoundation\RedirectResponse;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;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\CustomUserMessageAuthenticationException;use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;use Symfony\Component\Security\Http\Authenticator\Passport\Badge\PreAuthenticatedUserBadge;use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;use Symfony\Component\Security\Http\Authenticator\Passport\Passport;use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;use Symfony\Component\Security\Http\SecurityRequestAttributes;class LoginAuthenticator extends AbstractAuthenticator{public function __construct(private readonly ParameterBagInterface $parameterBag, private readonly LoggerInterface $logger, private readonly RouterInterface $router){}public function supports(Request $request): ?bool{return 'login' === $request->attributes->get('_route') || 'index' === $request->attributes->get('_route')&& $request->isMethod('POST');}/*** @throws GuzzleException*/public function authenticate(Request $request): Passport{$username = $request->request->get('username');$password = $request->request->get('password');$request->getSession()->set(SecurityRequestAttributes::LAST_USERNAME, $username);$client = new Client(['base_uri' => $this->parameterBag->get('api_url')]);$apiToken = null;try {$response = $client->post('/login', ['json' => ['username' => $username,'password' => $password]]);$apiToken = json_decode($response->getBody()->getContents());} catch (Exception $exception) {$this->logger->error($exception->getMessage());throw new CustomUserMessageAuthenticationException($exception->getMessage());}return new SelfValidatingPassport(new UserBadge($apiToken->token), [new PreAuthenticatedUserBadge()]);}public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response{/** @var User $user */$user = $token->getUser();if (in_array(User::ROLE_CONTRACTOR, $user->getRoles())) {$request->attributes->set(SecurityRequestAttributes::AUTHENTICATION_ERROR, new AuthenticationException('You don\'t have the right access for the admin side'));return null;}if ($user->getPasswordToken()) {$request->attributes->set(SecurityRequestAttributes::AUTHENTICATION_ERROR, new AuthenticationException('You have requested a password rest'));return null;}return new RedirectResponse($this->router->generate('app_homepage'));}public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response{if ($request->request->count() > 1) {$request->attributes->set(SecurityRequestAttributes::AUTHENTICATION_ERROR, new AuthenticationException('The username or password you’ve entered doesn’t match any account information we have on record. Please try again.'));}return null;}}