src/Security/AppAuthenticator.php line 20
<?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\Security;use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;use Symfony\Component\Security\Http\Authenticator\Passport\Badge\RememberMeBadge;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 AppAuthenticator extends AbstractLoginFormAuthenticator{use TargetPathTrait;final public const LOGIN_ROUTE = 'login';public function __construct(private readonly UrlGeneratorInterface $urlGenerator,private readonly Security $security,private readonly CustomUserProvider $userProvider){}public function authenticate(Request $request): Passport{$username = $request->request->get('username', '');$password = $request->request->get('password', '');$request->getSession()->set(Security::LAST_USERNAME, $username);return new Passport(new UserBadge($username, [$this->userProvider, 'loadUserByIdentifier']),new CustomCredentials(function ($credentials, $user) {return $this->userProvider->validatePassword($user, $credentials);},$password),[new CsrfTokenBadge('authenticate', $request->request->get('_csrf_token')),],[new RememberMeBadge(),]);}public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response{// For example:$currentUser = $this->security->getUser();if(in_array("ROLE_VENDOR",$currentUser->getRoles())){return new RedirectResponse($this->urlGenerator->generate('shop_vendor'));}elseif(in_array("ROLE_STORE",$currentUser->getRoles())){return new RedirectResponse($this->urlGenerator->generate('shop_store'));}elseif(in_array("ROLE_BACKOFFICE_AGENT",$currentUser->getRoles())){return new RedirectResponse($this->urlGenerator->generate('shop_agent'));}elseif(in_array("ROLE_SHIPPING_AGENT",$currentUser->getRoles())){return new RedirectResponse($this->urlGenerator->generate('shop_shipping_agent'));}elseif(in_array("ROLE_ADMIN",$currentUser->getRoles())){return new RedirectResponse($this->urlGenerator->generate('admin'));}else{return new RedirectResponse("/");}if ($targetPath = $this->getTargetPath($request->getSession(), $firewallName)) {return new RedirectResponse($targetPath);}//throw new \Exception('TODO: provide a valid redirect inside '.__FILE__);}protected function getLoginUrl(Request $request): string{return $this->urlGenerator->generate(self::LOGIN_ROUTE);}}