<?php
namespace App\Security;
use App\Enum\Role;
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\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\UserBadge;
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 LoginAuthenticator extends AbstractLoginFormAuthenticator
{
use TargetPathTrait;
public const LOGIN_ROUTE = 'app_login';
private UrlGeneratorInterface $urlGenerator;
private RouterInterface $router;
/**
* @param UrlGeneratorInterface $urlGenerator
* @param RouterInterface $router
*/
public function __construct(UrlGeneratorInterface $urlGenerator, RouterInterface $router)
{
$this->urlGenerator = $urlGenerator;
$this->router = $router;
}
public function authenticate(Request $request): Passport
{
$email = $request->request->get('email', '');
$request->getSession()->set(Security::LAST_USERNAME, $email);
return new Passport(
new UserBadge($email),
new PasswordCredentials($request->request->get('password', '')),
[
new CsrfTokenBadge('authenticate', $request->request->get('_csrf_token')),
]
);
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): Response
{
$user = $token->getUser();
try {
$roles = $user->getRoles();
if (in_array(Role::ROLE_ADMIN->getSymfonyRole(), $roles, true)) {
return new RedirectResponse($this->router->generate('app_panel_dashboard'));
}
if (in_array(Role::ROLE_SALES_REP->getSymfonyRole(), $roles, true)) {
return new RedirectResponse($this->router->generate('seller_dashboard'));
}
if (in_array(Role::ROLE_ACCOUNTING->getSymfonyRole(), $roles, true)) {
return new RedirectResponse($this->router->generate('accounting_dashboard'));
}
return new RedirectResponse($this->router->generate('app_panel_dashboard'));
} catch (\Exception $e) {
return new RedirectResponse($this->router->generate('login'));
}
}
// 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_panel_dashboard'));
//// throw new \Exception('TODO: provide a valid redirect inside '.__FILE__);
// }
protected function getLoginUrl(Request $request): string
{
return $this->urlGenerator->generate(self::LOGIN_ROUTE);
}
}