<?php
namespace App\EventListener;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
use Symfony\Component\Security\Core\Security;
class JWTCreatedListener
{
private Security $security;
/**
* @param Security $security
*/
public function __construct(Security $security)
{
$this->security = $security;
}
/**
* @param JWTCreatedEvent $event
*
* @return void
*/
public function onJWTCreated(JWTCreatedEvent $event): void
{
$payload = $event->getData();
$buildingIds = [];
$flats = $this->security->getUser()->getFlats();
foreach ($flats as $flat) {
$buildingId = $flat->getBuilding()->getId();
if (!in_array($buildingId, $buildingIds)) {
$buildingIds[] = $buildingId;
}
}
$payload['buildingId'] = $buildingIds;
$event->setData($payload);
$header = $event->getHeader();
$header['cty'] = 'JWT';
$event->setHeader($header);
}
}