src/Events/JWTAuthenticationFailureResponseSubscriber.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Events;
  3. use Exception;
  4. use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationFailureEvent;
  5. use Lexik\Bundle\JWTAuthenticationBundle\Response\JWTAuthenticationFailureResponse;
  6. use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  10. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  11. class JWTAuthenticationFailureResponseSubscriber implements EventSubscriberInterface
  12. {
  13.     public function __construct(private TokenStorageInterface $tokenStorage, private JWTTokenManagerInterface $JWTManager, private RequestStack $requestStack)
  14.     {
  15.     }
  16.     public function onLexikJwtAuthenticationOnAuthenticationFailure(AuthenticationFailureEvent $event): void
  17.     {
  18.         /** @var JWTAuthenticationFailureResponse $response */
  19.         $response $event->getResponse();
  20.         // My own processing where I used to have $event->getException()->getToken()
  21.         $response->setMessage("ERROR MESSAGE");
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             'lexik_jwt_authentication.on_authentication_failure' => 'onLexikJwtAuthenticationOnAuthenticationFailure',
  27.         ];
  28.     }
  29. }