Lexikjwtauthenticationbundle: Is there an easy way to add the userId to the JWT ?

Created on 20 Jan 2018  路  2Comments  路  Source: lexik/LexikJWTAuthenticationBundle

I'm using SF 4 and this bundle. It's working fine. I'm using the suggested config:

firewalls:
        login:
            pattern:  ^/api/login
            stateless: true
            anonymous: true
            form_login:
                check_path:               /api/login_check
                success_handler:          lexik_jwt_authentication.handler.authentication_success
                failure_handler:          lexik_jwt_authentication.handler.authentication_failure
                require_previous_session: false

when I decode the token in the frontend I see it has the username, but it does not have the userId. Is there an easy way of adding the userId to the token?

Most helpful comment

You can listen for lexik_jwt_authentication.on_jwt_created event and attach the userId to token payload

    public function onJwtCreated(JWTCreatedEvent $event): void
    {
        $user = $event->getUser();

        $payload = $event->getData();
        $payload['id'] = $user->getId();

        $event->setData($payload);
    }

All 2 comments

You can listen for lexik_jwt_authentication.on_jwt_created event and attach the userId to token payload

    public function onJwtCreated(JWTCreatedEvent $event): void
    {
        $user = $event->getUser();

        $payload = $event->getData();
        $payload['id'] = $user->getId();

        $event->setData($payload);
    }

Closing as explained

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pbalan picture pbalan  路  5Comments

rolandrajko picture rolandrajko  路  3Comments

ndoulgeridis picture ndoulgeridis  路  3Comments

garak picture garak  路  5Comments

astronati picture astronati  路  5Comments