Search code examples
phpsymfonyfosrestbundlelexikjwtauthbundle

Change default username field in LexikJWTAuthenticationBundle


I want to update my symfony 2.8 applcation to version 5.4.

I am using fos rest bundle for API as well as jwt auth bundle for authentication with token. My issue is that retrieving the token requires the _username field on version 2 and username on version 5.

Could you tell me how to modify this field so that it can correspond to my old configuration please?

lexik_jwt_authentication:
private_key_path: '%jwt_private_key_path%'
public_key_path:  '%jwt_public_key_path%'
pass_phrase:      '%jwt_key_pass_phrase%'
token_ttl:        '%jwt_token_ttl%'

My scurity.yaml

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

    refresh:
        pattern:  ^/api/token/refresh
        stateless: true
        anonymous: true

    api:
        pattern:   ^/api/secure
        stateless: true
        guard:
            authenticators:
                - word.user.guard.jwt_token_authenticator

    main:
        pattern: ^/
        form_login:
            provider: fos_userbundle
            csrf_token_generator: security.csrf.token_manager
        logout:       true
        anonymous:    true

Solution

  • You can change the default username field using the username_path parameter in the security.yaml configuration file:

    firewalls:
        login:
            pattern:  ^/api/login
            stateless: true
            anonymous: true
            form_login:
                username_path: YOUR_NEEDED_FIELD
                check_path:  /api/login_check