It seems like the issue is with the middleware registration. Instead of registering the middleware in the bootstrap/app.php file, try registering it in the app/Http/Kernel.php file. Here's how you can do it:
- Open the
app/Http/Kernel.phpfile and add the following code to the$routeMiddlewarearray:
'auth:api' => \Laravel\Passport\Http\Middleware\CheckClientCredentials::class,
-
Remove the
authmiddleware registration from thebootstrap/app.phpfile. -
In the
AuthServiceProviderclass, change theviaRequestmethod to use thepassportdriver instead of theapidriver:
$this->app['auth']->viaRequest('passport', function ($request) {
if ($request->header('Authorization')) {
$key = explode(' ', $request->header('Authorization'))[1];
return User::where('api_token', $key)->first();
}
});
- Finally, clear the cache by running the following command:
php artisan cache:clear
This should fix the issue and allow you to authenticate using Passport in Lumen.