When I'm doing the request to my symfony server running on http://localhost:8000/api/admin/login_check it returns the desired token.
However, when I do it with the functional tests I get the following error:
Error: Unable to find the controller for path \"\/api\/admin\/login_check\". The route is wrongly configured.
I also went through the functional test docs.
public function testLogin(){
$client = static::createClient();
$client->request('POST', '/api/admin/login_check', [], [],
[
'Content-Type' => 'application/json',
'Accept' => 'application/json'
],
json_encode([
'email' => '[email protected]',
'password' => 'qwerty123'
])
);
$this->assertEquals(200, $client->getResponse()->getStatusCode());
}
# Admin Routes
api_admin_login_check:
path: /api/admin/login_check
methods: [POST]
security:
# more configs here
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
login_admin:
pattern: ^/api/admin/login
stateless: true
anonymous: true
json_login:
username_path: email
provider: app_admin_provider
check_path: /api/admin/login_check
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
admin_api:
pattern: ^/api/admin
stateless: true
provider: app_admin_provider
guard:
authenticators:
- lexik_jwt_authentication.jwt_token_authenticator
access_control:
- { path: ^/api/admin/register, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api/admin/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api/admin, roles: ROLE_ADMIN }
- { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY }
What am I doing wrong?
Also any pointers as to why I'm getting a 401 JWT Token not found for my /api/admin/register even if I've put it in my access_control, would be nice.
Went through my logs and something fishy is effectively happening. When I do a postman request everything works well. Here is the logs for the request:
[2019-01-29 21:36:19] request.INFO: Matched route "api_admin_login". {"route":"api_admin_login","route_parameters":{"_route":"api_admin_login"},"request_uri":"https://localhost:8000/api/admin/login_check","method":"POST"} []
[2019-01-29 21:36:19] doctrine.DEBUG: SELECT t0.id AS id_1, t0.email AS email_2, t0.password AS password_3 FROM admin t0 WHERE t0.email = ? LIMIT 1 ["[email protected]"] []
[2019-01-29 21:36:19] security.INFO: User has been authenticated successfully. {"username":null} []
However, during my test here are the logs:
[2019-01-29 21:30:24] request.INFO: Matched route "api_admin_login". {"route":"api_admin_login","route_parameters":{"_route":"api_admin_login"},"request_uri":"http://localhost/api/admin/login_check","method":"POST"} []
[2019-01-29 21:30:24] security.INFO: Populated the TokenStorage with an anonymous Token. [] []
[2019-01-29 21:30:24] request.WARNING: Unable to look for the controller as the "_controller" parameter is missing. [] []
As can be seen it was unable to find the controller.
For the 401 error (on /api/register) I just simply needed to add this above my login_admin:
register_admin:
pattern: ^/api/admin/register
anonymous: true
stateless: true
Which is weird since I've already specified it in my Access Control.
I'm still getting the 404 not found error when accessing login_check.
@chalasr I've cloned your project and was able to reproduce the issue.
https://github.com/kemicofa/lexik-jwt-authentication-sandbox
When the project is installed and the Unit Tests run, there is a 404 error when testing the login_check route. However, retrieving the token via curl works perfectly fine.
I'm still currently trying to solve this issue. I think I'm slowly getting an idea of what might be causing this. These are the possibilities I've ruled out:
Functional tests have seem to worked pre Symfony4. I'm coming to the conclusion that Symfony4 must have changed something during testing so that the /login_check route doesn't get picked up by the lexik listener. Unsure and I'm speculating at this point.
It's also quite possible a symfony configuration or lexik configuration is required for this to work accordingly during testing.
Content-type should be CONTENT_TYPE
$client->request(
'POST',
'/login_check',
['_username' => 'lexik', '_password' => 'dummy'],
[],
['CONTENT_TYPE' => 'application/json']
);
Try to change order of your firewalls, example :
1 - dev
2 - login
3 - api
4 - main
Most helpful comment
Content-typeshould beCONTENT_TYPE