<?php
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class JwtSubscriber implements EventSubscriberInterface
{
public function onKernelRequest(RequestEvent $event)
{
$jwt = $event->getRequest()->getSession()->get('jwt');
if ($jwt && !empty($jwt['expired_from']) && $jwt['expired_from']->getTimestamp() > time()) {
$event->getRequest()->headers->add(['Authorization' => 'Bearer '.$jwt['jwt']]);
}
}
public static function getSubscribedEvents()
{
return [
// must be registered before (i.e. with a higher priority than) the default Locale listener
KernelEvents::REQUEST => [['onKernelRequest', 30]],
];
}
}