0
votes

I am trying to code a login function for my api that takes a username and password then give you a password grant token to make api requests. The login route when called gives you

{
    "message": "Unauthenticated."
}

I am using passport on laravel to do secure the api. Why am I getting a 401 when the route does not have the auth:api middleware? I tried using a clousure to see if I get could get a response and the closure did not give me an error.

Route::group(['prefix' => '/v1', 'middleware' => ['auth:api'], 'namespace' => 'Api\V1', 'as' => 'api.'], function () {
        Route::post('/post/like','PostLikeController@store');

});
Route::group(['prefix' => '/v1', 'namespace' => 'Api\V1', 'as' => 'api.'], function () {
        Route::post('login', 'Auth\LoginController@login');
});
2

2 Answers

0
votes

Does your login controller have a constructor? sometimes middleware is set in there?

Otherwise I've also had issues with having the middleware routes above the public ones.

Try putting the public routes in the file first and also checking the LoginController.php for a constructor which might be setting a middleware

0
votes

It possibly due to the same prefixes, as it does not overriding but instead stacking on top of each other.

I suggest for your login route, possibly, you can use this

Route::post('login', 'Auth\LoginController@login')->withoutMiddleware([FooMiddleware::class]);

If it's still does not help try putting your login route above the middlewared route.