APIPlatform JWT authentication_token route issue

I use Symfony 5 and React, with docker.

Both containers are on different docker-compose but on the same network, so they can see and ping each other.

I installed the bundle with :
docker-compose exec php composer require jwt-auth

Then generated public and private keys with

docker-compose exec php sh -c '
    set -e
    apk add openssl
    mkdir -p config/jwt
    jwt_passphrase=${JWT_PASSPHRASE:-$(grep ''^JWT_PASSPHRASE='' .env | cut -f 2 -d ''='')}
    echo "$jwt_passphrase" | openssl genpkey -out config/jwt/private.pem -pass stdin -aes256 -algorithm rsa -pkeyopt rsa_keygen_bits:4096
    echo "$jwt_passphrase" | openssl pkey -in config/jwt/private.pem -passin stdin -out config/jwt/public.pem -pubout
    setfacl -R -m u:www-data:rX -m u:"$(whoami)":rwX config/jwt
    setfacl -dR -m u:www-data:rX -m u:"$(whoami)":rwX config/jwt
'

So I got routes.yaml file with:

authentication_token:
    path: /authentication_token
    methods: ['POST']

And security.yaml with :

security:
    encoders:
        AppEntityUsers:
            algorithm: auto

    providers:
        app_user_provider:
            entity:
                class: AppEntityUsers
                property: email
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            stateless: true
            anonymous: true
            lazy: true
            provider: app_user_provider
            json_login:
                check_path: /authentication_token
                username_path: email
                password_path: password
                success_handler: lexik_jwt_authentication.handler.authentication_success
                failure_handler: lexik_jwt_authentication.handler.authentication_failure
            guard:
                authenticators:
                    - lexik_jwt_authentication.jwt_token_authenticator
    access_control:
        - { path: ^/docs, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/authentication_token, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/, roles: IS_AUTHENTICATED_FULLY }

Now if I try to hit this route with my react front, I got the following errors in my web browser

in console

Response {type: "cors", url: "https://localhost:8443/authentication_token", redirected: false, status: 404, ok: false, …}body: (...)bodyUsed: trueheaders: Headers {}ok: falseredirected: falsestatus: 404statusText: ""type: "cors"url: "https://localhost:8443/authentication_token"__proto__: Response

In network response

Unable to find the controller for path "/authentication_token". The route is wrongly configured

And if I try to access the route in the browser with :

https://localhost:8443/authentication_token

I get :

No route found for "GET /authentication_token": Method Not Allowed (Allow: POST)

I don’t understrand why, because I’ve followed api platform doc:
https://api-platform.com/docs/core/jwt/

I send my request through a POST on react :

const response = await fetch('https://localhost:8443/authentication_token', {
                credentials: 'include',
                headers: {
                    Accept: 'application/json',
                },
                method: 'POST',
                body: data,
            })

            console.warn(response)
            const responseData = await response.json()
            console.warn(responseData)
            onConnect(responseData)

And I’ve checked header request and response request, both are set to the POST method.

Someone has an idea on what I’ve missed ?

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

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