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');
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.
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.
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.
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:
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.
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.
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.
Get the user from the api
guard directly: auth()->guard('api')->user()
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With