Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passport - "Unauthenticated." - Laravel 5.3

I hope someone could explain why I'm unauthenticated when already has performed a successfull Oauth 2 authentication process.

I've set up the Passport package like in Laravel's documentation and I successfully get authenticated, receives a token value and so on. But, when I try to do a get request on, let say, /api/user, I get a Unauthenticated error as a response. I use the token value as a header with key name Authorization, just as described in the docs.

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

This function is suppose to give back my self as the authenticated user, but I'm only getting Unauthenticated. Likewise, if I just return the first user, I'm again getting Unauthenticated.

Route::get('/test', function(Request $request) {
    return App\User::whereId(1)->first();
})->middleware("auth:api");

In a tutorial from Laracast, guiding through the setup of Passport, the guider doesn't have the ->middleware("auth:api") in his routes. But if its not there, well then there's no need for authentication at all!

Please, any suggestions or answers are more then welcome!

like image 759
Rikard Olsson Avatar asked Aug 30 '16 12:08

Rikard Olsson


People also ask

How can I get Passport token in laravel?

Requesting Tokens Once you have created a password grant client, you may request an access token by issuing a POST request to the /oauth/token route with the user's email address and password. Remember, this route is already registered by the Passport::routes method so there is no need to define it manually.

What is Passport authentication in laravel?

Laravel Passport is an easy way to set up an authentication system for your API. As a Laravel package, it uses an OAuth2 server to perform authentication, creating tokens for user applications that request to interface with the API it protects, and only granting them access if their tokens are validated.

Is laravel Passport SSO?

Laravel Passport Single Sign On (SSO) for Your Application miniOrange provides a ready to use Single Sign On (SSO)solution for your application. This solution ensures that you are ready to roll out secure access to your application using Laravel Passport within minutes.


3 Answers

You have to set an expiration date for the tokens you are generating,

set the boot method in your AuthServiceProvider to something like the code below and try generating a new token. Passports default expiration returns a negative number

public function boot()
{
  $this->registerPolicies();
   Passport::routes();
   Passport::tokensExpireIn(Carbon::now()->addDays(15));
   Passport::refreshTokensExpireIn(Carbon::now()->addDays(30));
}
like image 121
Ossaija D Avatar answered Oct 19 '22 21:10

Ossaija D


Check your user model and the database table, if you have modified the primary id field name to say something other than "id" or even "user_id" you MIGHT run into issues. I debugged an issue regarding modifying the primary id field in my user model and database table to say "acct_id" instead of keeping it as just "id" and the result was "Unauthenticated" When I tried to get the user object via GET /user through the auth:api middleware. Keep in mind I had tried every other fix under the sun until I decided to debug it myself.

ALSO Be sure to UPDATE your passport. As it has had some changes made to it in recent weeks.

I'll link my reference below, it's VERY detailed and well defined as to what I did and how I got to the solution.

Enjoy!

https://github.com/laravel/passport/issues/151

like image 2
Andre F. Avatar answered Oct 19 '22 21:10

Andre F.


I had this error because of that I deleted passport mysql tables(php artisan migrate:fresh), php artisan passport:install helps me. Remember that after removing tables, you need to re-install passport!

like image 1
Илья Зеленько Avatar answered Oct 19 '22 22:10

Илья Зеленько