LexikJWTAuthenticationBundle: FOSUser + LexikJWT still returning 401 "Bad Credentials"

Hello, After reading and trying all fixes found in here trying to get LexikJWT and FOSUser working together, I finally create my own new issue because I could really not find a way, it’s still returning 401 “Bad Credentials”…

I need a “backend admin” login (which is working) independent from my API login which will be used in an hybrid app.

Here is my security.yml :

security:
    encoders:
        FOS\UserBundle\Model\UserInterface: sha512

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_API:         ROLE_USER
        ROLE_SUPER_ADMIN: ROLE_ADMIN

    providers:
        fos_userbundle:
            id: fos_user.user_provider.username_email

    firewalls:
        admin:
            pattern: ^/admin
            form_login:
                login_path: /admin/login
                check_path: /admin/login_check
                provider: fos_userbundle
                csrf_provider: form.csrf_provider
            logout:
                path:        /admin/logout
                target:      /admin
            anonymous:    true

        dev:
            pattern:  ^/(_(profiler|wdt)|css|images|js)/
            security: false

        login:
            pattern:  ^/api/login
            stateless: true
            anonymous: true
            provider: fos_userbundle
            form_login:
                check_path:     /api/login_check
                username_parameter: username
                password_parameter: password
                require_previous_session: false
                success_handler: lexik_jwt_authentication.handler.authentication_success
                failure_handler: lexik_jwt_authentication.handler.authentication_failure

        api:
            pattern:   ^/api
            stateless: true
            provider: fos_userbundle
            lexik_jwt:
                authorization_header:
                    enabled: true
                    prefix:  Bearer
                throw_exceptions:        false
                create_entry_point:      true

    access_control:
        - { path: ^/admin/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/, role: ROLE_ADMIN }
        - { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/api, roles: [IS_AUTHENTICATED_FULLY, ROLE_API, ROLE_ADMIN] }

If someone has any idea why I cannot manage to get this working, I thank you in advance.

I’m trying using Sandbox’s AngularJS implementation and Curl CLI, they both return 401 Bad Credentials…

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 44 (6 by maintainers)

Most upvoted comments

Well… I’ve just faced the same problem now and I fixed it by creating an empty file src/Repository/UserRepository.php because I’m using src/Entity/User.php . As simple as it sounds… I hope it might help somebody…

I found the problem by debugging the exception returned in the construct

Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationFailureEvent

In my case the exception was coming from FOSuserbundle and telling me no username!