Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.6 passport api authentication not working in get request

Tags:

php

laravel

api

I am making single page application and while processing get request, i have passed X-CSRF-TOKEN and X-Requested-With headers.I also have included \Laravel\Passport\Http\Middleware\CreateFreshApiToken::class in web middleware group. my route in api.php looks like

Route::get('categories','Api\Categories@index')->middleware('auth:api');

but requesting specified url shows unauthenticated message.

like image 534
salin kunwar Avatar asked Apr 23 '18 10:04

salin kunwar


2 Answers

I got the exact same problem a while ago. And this is what I've done to fix it, more details in this post: https://github.com/laravel/passport/issues/47

So this is normally to fix the oAuth Client tokens: The expiry date is set to now + 100 years in Passport.php, line 167.

return static::$tokensExpireAt? Carbon::now()->diff(static::$tokensExpireAt): new DateInterval('P100Y');

If you set it to, i.e., P1Y, it is working. Something like:

return static::$tokensExpireAt? Carbon::now()->diff(static::$tokensExpireAt): new DateInterval('P1Y');

The same holds true for the refresh token a few lines below:

return static::$refreshTokensExpireAt? Carbon::now()->diff(static::$refreshTokensExpireAt): new DateInterval('P1Y');

And this is for the Personal Tokens: And also in PassportServiceProvider.php line 84, concerning the Personal Tokens:

$server->enableGrantType(new PersonalAccessGrant, new DateInterval('P1Y'));

Hope it helps ! :)
like image 195
Priyank lohan Avatar answered Sep 22 '22 13:09

Priyank lohan


Send authorisation header in the request

Authorization: Bearer eYz-your-passport-generated-token
like image 26
Jagadesha NH Avatar answered Sep 25 '22 13:09

Jagadesha NH