src/Component/Subscriber/AuthenticationSubscriber.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\Component\Subscriber;
  3. use App\Component\Event\AuthenticationEvent;
  4. use App\Component\User\UserService;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  8. class AuthenticationSubscriber implements EventSubscriberInterface
  9. {
  10.     public function __construct(
  11.         private UrlGeneratorInterface $urlGenerator,
  12.         private UserService $userService
  13.     )
  14.     {
  15.     }
  16.     public static function getSubscribedEvents(): array
  17.     {
  18.         return [
  19.             AuthenticationEvent::class => 'onAuthenticate'
  20.         ];
  21.     }
  22.     public function onAuthenticate(AuthenticationEvent $event): void
  23.     {
  24.         $user $event->getUser();
  25.         $route $this->userService->getDashboardRoute($user);
  26.         $url $this->urlGenerator->generate($route);
  27.         $response = new RedirectResponse($url);
  28.         $event->setResponse($response);
  29.     }
  30. }