Does your request include the 'Accept application/json' header?
API with Passport Validation and Error Handling
Im having some issues regarding validating the requests, laravel Route api.php is acting like Route web.php.
My api routes is:
Route::post('login', 'PassportController@login');
Route::post('register', 'PassportController@register');
Route::middleware('auth:api')->group(function () {
//USERS
Route::get('get-details', 'UserController@getDetails');
});
I configured all good passport has the documentation told me, and works fine, but now im testing my request where is not included the token to validate and error handling.
My Route:
http://localhost:8000/api/get-details
My User Controller:
public function getDetails()
{
if(!Auth::check())
{
return response()->json(['error' => 'UnAuthorised'], 401);
}
//Get Auth User
$user = Auth::user();
//Return 404 if user not found
if(is_null($user)){
return response()->json(null,404);
}
return response()->json($user, 200);
}
Than using Postman or other tool, i added my route iwthout the token to than case if the token is not included give me a json error message. But insted gives me this error:
InvalidArgumentException
Route [login] not defined.
And i notice the issue was in the Midleware Authenticate.php:
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
return route('login');
}
}
Where 'expectsJson' is giving me false. How can i resolve the issue? Many examples i see online tells overwriding the Request base or commenting the return route login, but this is not a solution for me since in my case im having in the same App the API routes where my mobile app communicates with it, and then ill have a web backend to manage the information.
Can someone advise me how to solve the issue?
Please or to participate in this conversation.