Skip to content

Event never happends #1287

@AlexSearha

Description

@AlexSearha

AuthenticationSuccessEvent not triggered on login

Hello and thanks for this bundle,

I am using Symfony 7 with LexikJWTAuthenticationBundle. I configured a subscriber for AuthenticationSuccessEvent and AuthenticationFailureEvent, but when I attempt to login via my /api/login endpoint, the subscriber is never called.

Setup

Symfony version: 7

Firewall configuration:

firewalls:
    api:
        pattern: ^/api
        stateless: true
        json_login:
            check_path: /api/login
            username_path: email
            password_path: password
            success_handler: lexik_jwt_authentication.handler.authentication_success
            failure_handler: lexik_jwt_authentication.handler.authentication_failure

Subscriber implementation:

<?php

namespace App\EventSubscriber;

use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationFailureEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class TestLexikSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            AuthenticationSuccessEvent::class => 'onAuthenticationSuccessEvent',
            AuthenticationFailureEvent::class => 'onAuthenticationFailureEvent',
        ];
    }

    public function onAuthenticationSuccessEvent(AuthenticationSuccessEvent $event): void
    {
        // Debug to check if the event is triggered
        dd($event, 'success');
    }

    public function onAuthenticationFailureEvent(AuthenticationFailureEvent $event): void
    {
        // Debug to check if the event is triggered
        dd($event, 'fail');
    }
}

Routes configuration:

controllers:
  resource:
    path: ../src/Controller/
    namespace: App\Controller
  type: attribute

api_login_check:
  path: /api/login

api_refresh_token:
  path: /api/token/refresh

api_token_invalidate:
  path: /api/token/invalidate

Problem

Even though I POST credentials to /api/login, neither of the subscriber methods is called.
The JWT is generated correctly if I use the default success handler, but my subscriber never receives the event.
Autoconfiguration is enabled, and the subscriber is correctly loaded.

Questions

  1. Should AuthenticationSuccessEvent be triggered automatically when using json_login in Symfony 7?
  2. Are there any changes in Symfony 7 or the bundle that would prevent the event from being dispatched?
  3. Is there a recommended way to ensure my subscriber is triggered on successful login?

thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions