Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel passport: Request user() returning null outside auth:api middleware, and inside returning user object

Tags:

php

laravel

When I am tring to get loggedin user details using auth:api middleware, it returns user object with details in my controller function.

api.php (with auth:api middleware returns User object)

Route::group(['middleware' => 'auth:api'], function() {
    Route::get('users/mentor_details/{uuid}','UserController@getMentorProfileDetails');
});

But when I am trying to get loggedin user details outside this auth:api middleware, it returns null.

api.php (without auth:api middleware return null)

Route::get('users/mentor_details/{uuid}','UserController@getMentorProfileDetails');
like image 576
NAVEEN KUMAR Avatar asked Sep 08 '17 06:09

NAVEEN KUMAR


People also ask

What is auth () in laravel?

Laravel includes built-in authentication and session services which are typically accessed via the Auth and Session facades. These features provide cookie-based authentication for requests that are initiated from web browsers. They provide methods that allow you to verify a user's credentials and authenticate the user.

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.

What is OAuth2 authentication in laravel?

Laravel Passport is an OAuth 2.0 server implementation for API authentication using Laravel. Since tokens are generally used in API authentication, Laravel Passport provides an easy and secure way to implement token authorization on an OAuth 2.0 server.


2 Answers

When the auth middleware is not provided, or is provided without specifying the guard, the default guard is used to determine the user. Unless you have changed this in your config/auth.php file, the default guard is the web guard.

So, when you go to a route that is not protected by a specific auth middleware, the user that is loaded is the one provided by the web guard.

Therefore, even though you may be sending the bearer token to use a specific user, the web guard doesn't know anything about that, and since you have no user logged in via the web guard, you are getting a null user.

You've got four options:

  1. Make sure the route is protected by the auth:api middleware, which specifies the api guard. This, however, will not allow guests to access the url.

  2. Change your default guard to api in your config/auth.php file. This is probably not what you want to do, especially if you do have normal web users.

  3. Tell the request you want the user from the api guard. The $request->user() method takes a guard as an argument, so if you do $request->user('api'), it will retrieve the user using the api guard.

  4. Get the user from the api guard directly: auth()->guard('api')->user().

like image 102
patricus Avatar answered Oct 19 '22 11:10

patricus


The auth middleware is the one returning the user. auth:api just indicates to use the API guard. In the source code of laravel, the file vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php line 62, the function shouldUse is the one setting the Auth::user() object. Check out also vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php shouldUse function

like image 21
leyduana Avatar answered Oct 19 '22 11:10

leyduana