src/EventListener/LoginSuccessListener.php line 25
<?php
namespace App\EventListener;
use JetBrains\PhpStorm\ArrayShape;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Http\SecurityEvents;
use Symfony\Component\Routing\RouterInterface;
class LoginSuccessListener implements EventSubscriberInterface
{
public function __construct(private readonly RouterInterface $router)
{}
#[ArrayShape([SecurityEvents::INTERACTIVE_LOGIN => "string"])] public static function getSubscribedEvents(): array
{
return [
SecurityEvents::INTERACTIVE_LOGIN => 'onSecurityInteractiveLogin',
];
}
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event): RedirectResponse
{
$user = $event->getAuthenticationToken()->getUser();
$roles = $user->getRoles();
if (in_array("ROLE_ADMIN", $roles) || in_array("ROLE_OPERATOR", $roles)) {
return new RedirectResponse($this->router->generate('admin_order'));
} elseif (in_array("ROLE_USER", $roles)) {
return new RedirectResponse($this->router->generate('web_homepage'));
} else {
return new RedirectResponse($this->router->generate('web_homepage'));
}
}
}