src/Module/Firewall/EventSubscriber/LoginRedirectSubscriber.php line 32

  1. <?php
  2. namespace App\Module\Firewall\EventSubscriber;
  3. use Psr\Log\LoggerInterface;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpFoundation\RequestStack;
  7. use Symfony\Component\Routing\RouterInterface;
  8. use Symfony\Component\Security\Http\Event\LoginSuccessEvent;
  9. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  10. class LoginRedirectSubscriber implements EventSubscriberInterface
  11. {
  12.     use TargetPathTrait;
  13.     public function __construct(
  14.         protected RouterInterface $router,
  15.         protected LoggerInterface $logger,
  16.         protected RequestStack $requestStack
  17.     )
  18.     {}
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             LoginSuccessEvent::class => ['onLoginSuccess'5]
  23.         ];
  24.     }
  25.     public function onLoginSuccess(LoginSuccessEvent $event): void
  26.     {
  27.         if ($event->getFirewallName() != 'main'
  28.             || $this->requestStack->getCurrentRequest()->headers->has('authorization')
  29.         )
  30.         {
  31.             return;
  32.         }
  33.             $event->setResponse(new RedirectResponse(
  34.                 $this->router->generate('app_module_firewall_firewall_list__invoke')
  35.             ));
  36.     }
  37. }