Laravel 5.4 mongodb passport access token Unauthenticated
NickName:Viet Nguyen Ask DateTime:2017-05-17T12:27:30

Laravel 5.4 mongodb passport access token Unauthenticated

I've built an example Laravel project using mongodb (Jenssegers Mongodb) and I use passport in Laravel 5.4 follow by this document.

Everything work fine until I took an access token to Postman for test, now take a look at my api.php route

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

In postman, I setup two headers that are Accept: application/json and Authorization: Bearer $TOKEN and I'm very sure that my access token is not the copy missing fault but still getting error.

{
    "error": "Unauthenticated."
}

Things I've tried

I have overwrite default id field in model User.php

use Authenticatable, Authorizable, CanResetPassword, Notifiable, HasApiTokens;

protected $collection = 'users';
protected $fillable = ['username', 'email', 'password', 'name'];
protected $primaryKey = '_id';

I also modify the token expiration time in AuthserviceProvider.php like so

public function boot()
{
    $this->registerPolicies();

    Passport::routes();
    Passport::tokensExpireIn(Carbon::now()->addYears(20));//You can also use addDays(10)
    Passport::refreshTokensExpireIn(Carbon::now()->addYears(20));//You can also use addDays(10)
    Passport::pruneRevokedTokens(); //basic garbage collector

    Passport::tokensCan([
        'conference' => 'Access your conference information'
    ]);
}

And some other ways but still not work.

UPDATE for debug infomation

When I add try catch in to public/index.php, an error apperead

League\OAuth2\Server\Exception\OAuthServerException: The resource owner or authorization server denied the request. in /data/www/public_html/xxxx/vendor/league/oauth2-server/src/Exception/OAuthServerException.php:165
Stack trace:
#0 /data/www/public_html/xxxx/vendor/league/oauth2-server/src/AuthorizationValidators/BearerTokenValidator.php(66): League\OAuth2\Server\Exception\OAuthServerException::accessDenied('Access token ha...')
#1 /data/www/public_html/xxxx/vendor/league/oauth2-server/src/ResourceServer.php(82): League\OAuth2\Server\AuthorizationValidators\BearerTokenValidator->validateAuthorization(Object(Zend\Diactoros\ServerRequest))
......

When I check file vendor\league\oauth2-server\src\AuthorizationValidators\BearerTokenValidator.php at line 66. Seems my access token has been revoked, but in my database revoked column still false, and this access token is brand new, I just created in few minutes ago.

if ($this->accessTokenRepository->isAccessTokenRevoked($token->getClaim('jti'))) {
    throw OAuthServerException::accessDenied('Access token has been revoked');
}

Any idea?

Copyright Notice:Content Author:「Viet Nguyen」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/44015582/laravel-5-4-mongodb-passport-access-token-unauthenticated

More about “Laravel 5.4 mongodb passport access token Unauthenticated” related questions

Laravel 5.4 mongodb passport access token Unauthenticated

I've built an example Laravel project using mongodb (Jenssegers Mongodb) and I use passport in Laravel 5.4 follow by this document. Everything work fine until I took an access token to Postman for...

Show Detail

Laravel 5.4 Passport Personal Token Postman Error Unauthenticated

I am using Laravel 5.4 Passport for personal tokens. I am trying to use Postman to send a GET request to /api/user. I get my token by sending a GET request to /oauth/personal-access-tokens. From that

Show Detail

Laravel 5.4 passport axios always returns Unauthenticated

I've followed the guide here:https://laravel.com/docs/5.4/passport#consuming-your-api-with-javascript Using axios: ... mounted: function() { axios.get('/api/user') .t...

Show Detail

Passport laravel returns Unauthenticated always

I am using laravel/passport for the first time. I did the following steps in my existing code package. composer require laravel/passport php artisan migrate Included piece of code as follows //app/

Show Detail

Laravel Passport Access Token Always Unauthenticated error

When I test the access token using postman, it always return user unauthenticated when accessing http://passport.dev/api/user. I check all the spaces of access token, the right headers, Authorizat...

Show Detail

Not expired Laravel passport token

I'm developing standard Laravel REST API. I'm following the Laravel 5.4 API Authentication (Passport) Documentation. What I need is, User should log in through API and get access token which will ...

Show Detail

Laravel 5.4 401/Unauthenticated using Passport and multiple token types

The backstory on this, I have been working on the instructions from the documentation: https://laravel.com/docs/5.4/passport I have Laravel 5.4 "laravel/passport": "^3.0" from composer Local Mac ...

Show Detail

Passport oauth token path not found in laravel5.4

In laravel5.4 passport authentication is not working. there are error about in in oauth /token file may not found where is this file path #oauth #laravel #token #passport #authentication

Show Detail

Laravel 5.4 how to change passport error token response

I using Laravel Passport Authentication.Laravel Response on token error is only { "error": "Unauthenticated." } but i want different response for token error like { "error" : t

Show Detail

Laravel 5.4 Passport: Modal Login

I am new to laravel and I am trying to use a modal to let users login to a website. I set up laravel passport as described here in the docs. Now if I am entering the credentials of an existing us...

Show Detail