i use Laravel passport for auth
in route api.php
Route::get(\'/todos\', function(){
return \'hello\';
})->middleware(\'auth:api\');
In the following of @Eki answer,
This error is because you didn't set "Accept" field in your headers.
To avoid this error, add a middleware with priority to Authenticate to check that:
add an extra middleware with below handler
public function handle($request, Closure $next)
{
if(!in_array($request->headers->get('accept'), ['application/json', 'Application/Json']))
return response()->json(['message' => 'Unauthenticated.'], 401);
return $next($request);
}
set priority in app/Http/Kernel.php
protected $middlewarePriority = [
...
\App\Http\Middleware\MyMiddleware::class, // new middleware
\App\Http\Middleware\Authenticate::class,
...
];
add new middleware to your route
Route::get('/todos', function(){
return 'hello';
})->middleware('MyMiddleware', 'auth:api');