src/EventListener/JWTCreatedListener.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
  4. use Symfony\Component\Security\Core\Security;
  5. class JWTCreatedListener
  6. {
  7.     private Security $security;
  8.     /**
  9.      * @param Security $security
  10.      */
  11.     public function __construct(Security $security)
  12.     {
  13.         $this->security $security;
  14.     }
  15.     /**
  16.      * @param JWTCreatedEvent $event
  17.      *
  18.      * @return void
  19.      */
  20.     public function onJWTCreated(JWTCreatedEvent $event): void
  21.     {
  22.         $payload       $event->getData();
  23.         $buildingIds = [];
  24.         $flats $this->security->getUser()->getFlats();
  25.         foreach ($flats as $flat) {
  26.             $buildingId $flat->getBuilding()->getId();
  27.             if (!in_array($buildingId$buildingIds)) {
  28.                 $buildingIds[] = $buildingId;
  29.             }
  30.         }
  31.         $payload['buildingId'] = $buildingIds;
  32.         $event->setData($payload);
  33.         $header        $event->getHeader();
  34.         $header['cty'] = 'JWT';
  35.         $event->setHeader($header);
  36.     }
  37. }