harish durga
harish durga

Reputation: 524

Auth driver [passport] for guard [api] is not defined

I am using Laravel 5.7 and passport for oauth2. When I am trying to access the /api/user route with postman I am getting this error.

Upvotes: 1

Views: 10391

Answers (6)

Matej Petric
Matej Petric

Reputation: 158

It's a stupid solution, but I just had the same situation, so maybe, someone is going to find this useful.

I was using php artisan clear-compiled command in my deployment script which cleared generated passport keys, so whenever I was calling any route that's under auth middleware, I was getting this error.

So, the solution is to just call php artisan passport:install command again and everything is going to be working normally.

Upvotes: 0

user13376003
user13376003

Reputation:

this problem also occurs if you capitalize "passport" in config/auth.php :

correct :

'api' => [
    'driver' => 'passport',

wrong :

'api' => [
    'driver' => 'Passport',

notice that first letter of passport should not be capitalized !

Upvotes: 0

Abdenour Keddar
Abdenour Keddar

Reputation: 121

I run into a similar situation, you need to configure your driver to passport inside your config/auth.php file. For my case, I found a dummy command "passportphp artisan passport:keys instead of "passport""

        'api' => [
        'driver' => 'passport',
        'provider' => 'users',
        'hash' => false,
    ],

Next check carefully your headers and be sure you set

Authorization : "Bearer + token" and Accept to Application/json

Upvotes: 0

harish durga
harish durga

Reputation: 524

I found the problem. Actually I am using Swoole server rather than the php-fpm. I have to add the Passport class to the provides array in swoole configuration file swoole_http.php .

Upvotes: 3

Ismoil  Shifoev
Ismoil Shifoev

Reputation: 6021

Passport includes an authentication guard that will validate access tokens on incoming requests. Once you have configured the api guard to use the passport driver, you only need to specify the auth:api middleware on any routes that require a valid access token:

Route::get('/user', function () {
    //
})->middleware('auth:api');

When calling routes that are protected by Passport, your application's API consumers should specify their access token as a Bearer token in the Authorization header of their request. For example, when using the Guzzle HTTP library:

$response = $client->request('GET', '/api/user', [
    'headers' => [
        'Accept' => 'application/json',
        'Authorization' => 'Bearer '.$accessToken,
    ],
]);

Upvotes: 1

Kapitan Teemo
Kapitan Teemo

Reputation: 2164

Maybe you haven't setup your guard.

Please check you guard driver setup into config/auth.php first.

Upvotes: 0

Related Questions