Thursday, May 3, 2018

Laravel 5.6 passport api authentication not working in get request

Leave a Comment

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.

2 Answers

Answers 1

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 ! :) 

Answers 2

Send authorisation header in the request

Authorization: Bearer eYz-your-passport-generated-token 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment