src/EventListener/LoginSuccessListener.php line 25

  1. <?php
  2. namespace App\EventListener;
  3. use JetBrains\PhpStorm\ArrayShape;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  7. use Symfony\Component\Security\Http\SecurityEvents;
  8. use Symfony\Component\Routing\RouterInterface;
  9. class LoginSuccessListener implements EventSubscriberInterface
  10. {
  11.     public function __construct(private readonly RouterInterface $router)
  12.     {}
  13.     #[ArrayShape([SecurityEvents::INTERACTIVE_LOGIN => "string"])] public static function getSubscribedEvents(): array
  14.     {
  15.         return [
  16.             SecurityEvents::INTERACTIVE_LOGIN => 'onSecurityInteractiveLogin',
  17.         ];
  18.     }
  19.     public function onSecurityInteractiveLogin(InteractiveLoginEvent $event): RedirectResponse
  20.     {
  21.         $user $event->getAuthenticationToken()->getUser();
  22.         $roles $user->getRoles();
  23.         if (in_array("ROLE_ADMIN"$roles) || in_array("ROLE_OPERATOR"$roles)) {
  24.             return new RedirectResponse($this->router->generate('admin_order'));
  25.         } elseif (in_array("ROLE_USER"$roles)) {
  26.             return new RedirectResponse($this->router->generate('web_homepage'));
  27.         } else {
  28.             return new RedirectResponse($this->router->generate('web_homepage'));
  29.         }
  30.     }
  31. }