src/EventSubscriber/AuthenticationFailedSubscriber.php line 30

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventSubscriber;
  4. use JetBrains\PhpStorm\ArrayShape;
  5. use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationFailureEvent;
  6. use Psr\Log\LoggerInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use function json_decode;
  9. final class AuthenticationFailedSubscriber implements EventSubscriberInterface
  10. {
  11.     public function __construct(
  12.         private readonly LoggerInterface $logger,
  13.     )
  14.     {
  15.     }
  16.     #[ArrayShape(['lexik_jwt_authentication.on_authentication_failure' => "string"])]
  17.     public static function getSubscribedEvents(): array
  18.     {
  19.         return [
  20.             'lexik_jwt_authentication.on_authentication_failure' => 'onAuthenticationFailure',
  21.         ];
  22.     }
  23.     public function onAuthenticationFailure(AuthenticationFailureEvent $event)
  24.     {
  25.         $content json_decode($event->getRequest()->getContent(), true);
  26.         $username $content['username'] ?? '';
  27.         $this->logger->error(
  28.             sprintf(
  29.                 "The user \"%s\" was unable to login.  Exception: \"%s\"  Trace: %s",
  30.                 $username,
  31.                 $event->getException()->getMessage(),
  32.                 $event->getException()->getTraceAsString(),
  33.             ),
  34.         );
  35.     }
  36. }