Sodiki
Sodiki

Reputation: 161

jwt lexik JWT Authetication not found

I have been looking for a solution for a while now. All authentication system is in place, I get my token but when I use it to retrieve the data I get a 404 jwt not found.

I have an emergency on this project, a help will help me a lot.

this is my security.yaml:

app_user_provider:
        entity:
            class: App\Entity\User
            property: email
firewalls:
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false

    login:
        pattern: ^/api/login
        stateless: true
        json_login:
            check_path: /api/login_check # or api_login_check as defined in config/routes.yaml
            success_handler: lexik_jwt_authentication.handler.authentication_success
            failure_handler: lexik_jwt_authentication.handler.authentication_failure
    api:
        pattern:   ^/api
        stateless: true
        guard:
            authenticators:
                - lexik_jwt_authentication.jwt_token_authenticator
    

    main:
        lazy: true
        provider: app_user_provider

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

        # 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:
    # - { path: ^/admin, roles: ROLE_ADMIN }
    # - { path: ^/profile, roles: ROLE_USER }
    - { path: ^/api/login, roles: PUBLIC_ACCESS }
    - { path: ^/api,       roles: IS_AUTHENTICATED_FULLY }

Pour la declaration du controller:

api_login_check:
  path: /api/login_check

Upvotes: 1

Views: 1846

Answers (1)

Curtis Lanz
Curtis Lanz

Reputation: 445

You have to do the following; go to /config/packages/lexik_jwt_authentication.yaml

lexik_jwt_authentication:
    secret_key: '%env(resolve:JWT_SECRET_KEY)%'
    public_key: '%env(resolve:JWT_PUBLIC_KEY)%'
    pass_phrase: '%env(JWT_PASSPHRASE)%'

    token_extractors:
        authorization_header:
                        enabled: true
                        prefix:  Bearer
                        name:    Authorization
        cookie: // if you are using cookie
                enabled: true
                name: cookie_name // set the cookie name

If you are using httpClient; then you can Authorization: 'Bearer ' .$token in your header as follows;

 $headers = [
            'Authorization' => 'Bearer '.$token,
            'Content-Type' => 'application/json',
        ];

NOTE: make sure you have jwt directory, where you have private and public keys.

Upvotes: 2

Related Questions