src/EventSubscriber/AuthenticationSuccessSubscriber.php line 28

  1. <?php
  2. /*
  3.  * This file is part of the Pellipop project.
  4.  *
  5.  * (c) Mobizel
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace App\EventSubscriber;
  12. use ApiPlatform\Api\IriConverterInterface;
  13. use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
  14. use Lexik\Bundle\JWTAuthenticationBundle\Events;
  15. use Sylius\Component\Customer\Model\CustomerAwareInterface;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. final class AuthenticationSuccessSubscriber implements EventSubscriberInterface
  18. {
  19.     public function __construct(private IriConverterInterface $iriConverter)
  20.     {
  21.     }
  22.     public function onAuthenticationSuccessResponse(AuthenticationSuccessEvent $event): void
  23.     {
  24.         $data $event->getData();
  25.         $user $event->getUser();
  26.         if (!$user instanceof CustomerAwareInterface) {
  27.             return;
  28.         }
  29.         $data['customer'] = $this->iriConverter->getIriFromResource($user->getCustomer());
  30.         $event->setData($data);
  31.     }
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [Events::AUTHENTICATION_SUCCESS => 'onAuthenticationSuccessResponse'];
  35.     }
  36. }