src/EventSubscriber/JwtSseSubscriber.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Firebase\JWT\JWT;
  4. use Symfony\Component\HttpKernel\KernelEvents;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\Dotenv\Dotenv;
  7. use Symfony\Component\HttpFoundation\Cookie;
  8. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  9. class JwtSseSubscriber implements EventSubscriberInterface
  10. {
  11.     public function __construct() {}
  12.     public static function getSubscribedEvents()
  13.     {
  14.         return [
  15.             KernelEvents::RESPONSE => 'setJwt',
  16.             // KernelEvents::TERMINATE : Appelé une seul fois après le traitement de la rêquete, evite les doublons dans la dbb
  17.         ];
  18.     }
  19.     public function setJwt(ResponseEvent $event)
  20.     {
  21.         $response $event->getResponse();
  22.         $dotenv = new Dotenv();
  23.         $dotenv->load(__DIR__ '/../../.env');
  24.         $key $_ENV['MERCURE_JWT_SECRET'];
  25.         $head = [
  26.             "alg" => "HS256",
  27.             "typ" => "JWT"
  28.         ];
  29.         $payload = [
  30.             'mercure' => [
  31.                 // 'subscribe' => [
  32.                 //     $user->getFlux()->getTopicLabel(),
  33.                 //     // 'GLOBALTOPIC',
  34.                 //     // 'http://monsite.fr/message',
  35.                 //     // 'http://monsite.fr/user/'.$this->getUser()->getUsername()
  36.                 // ],
  37.                 // 'publish' => [ // publish add also to subcribse
  38.                 //     'http://monsite.fr/message',
  39.                 //     'http://monsite.fr/user/'.$this->getUser()->getUsername()
  40.                 // ],
  41.                 'subscribe' => [
  42.                     '*'
  43.                 ],
  44.                 'publish' => [ // publish add also to subcribse
  45.                     '*'
  46.                 ],
  47.             ],
  48.         ];
  49.         $jwt JWT::encode($payload$key'HS256'null$head);
  50.         // $response = new RedirectResponse('/social', 302);
  51.         // $response = $this->forward('App\Controller\HomeController::index', []);
  52.         // $response = new Response();
  53.         // $response->headers->set('set-cookie', "mercureAuthorization={$jwt}; HttpOnly;");
  54.         $response->headers->setCookie(Cookie::create('mercureAuthorization'$jwt));
  55.     }
  56. }