src/EventSubscriber/JwtSubscriber.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\RequestEvent;
  5. use Symfony\Component\HttpKernel\KernelEvents;
  6. class JwtSubscriber implements EventSubscriberInterface
  7. {
  8.     public function onKernelRequest(RequestEvent $event)
  9.     {
  10.         $jwt $event->getRequest()->getSession()->get('jwt');
  11.         if ($jwt && !empty($jwt['expired_from']) && $jwt['expired_from']->getTimestamp() > time()) {
  12.             $event->getRequest()->headers->add(['Authorization' => 'Bearer '.$jwt['jwt']]);
  13.         }
  14.     }
  15.     public static function getSubscribedEvents()
  16.     {
  17.         return [
  18.             // must be registered before (i.e. with a higher priority than) the default Locale listener
  19.             KernelEvents::REQUEST => [['onKernelRequest'30]],
  20.         ];
  21.     }
  22. }