Symfony connexion jwt and admin connexion

Symfony connexion jwt and admin connexion

I want to create a Symfony API with admin space.
I have my admin user who can connect with the classic way of Symfony.
And my users to connect with the LexikJWTAuthenticationBundle, the app give a token to the user who can be used to my future front-app.

But my probleme is: Symfony can’t have two different login system? One classic and one other with JWT? Or am I doing something wrong?

In my security.yaml, Symfony doesn’t understand my route /api/login_check because the main overwrite the JWT route login.

security:
    encoders:
        AppEntityUser:
            algorithm: auto

    # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
    providers:
        # used to reload user from session & other features (e.g. switch_user)
        app_user_provider:
            entity:
                class: AppEntityUser
                property: email

    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            anonymous: lazy
            guard:
                authenticators:
                    - AppSecurityAdminAuthenticator
            logout:
                path: app_logout
                # where to redirect after logout
                # target: app_any_route

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

        api:
            pattern:   ^/api
            stateless: true
            provider: app_user_provider
            guard:
                authenticators:
                    - lexik_jwt_authentication.jwt_token_authenticator

            # activate different ways to authenticate
            # https://symfony.com/doc/current/security.html#firewalls-authentication

            # https://symfony.com/doc/current/security/impersonating_user.html
            # switch_user: true

    # Easy way to control access for large sections of your site
    # Note: Only the *first* access control that matches will be used
    access_control:
        # admin space
        - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/dashboard, roles: ROLE_ADMIN }
        # user jwt to access /api/...
        - { path: ^/api/login_check, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/api, roles: IS_AUTHENTICATED_FULLY }

Do you have some ideas or tips ?
(its my first post on stackoverflow, be kind pls, i learn)

Source: Symfony Questions

Leave a Reply

Your email address will not be published. Required fields are marked *