Reputation: 3750
I've installed Passport with Laravel 5.3 and configured to use Personal Access Token. I am sending username and password to `/oauth/token' route and get a token. Everything is fine until here.
But if I want to get current logged in user with $user = Auth::user();
as we used to get it before, I get null
value as Laravel don't keep any session for API Token. In this situation, how can I get the current user? Can I modify any file to get the current user along with token?
Thanks In Advance Arif
Upvotes: 4
Views: 3030
Reputation: 3234
First of all, I'm using Passport with password_clients in an OAuth2 flow. My Lumen/Laravel version in composer is 5.4.*
I'm using the following statement in my Lumen application (note I'm not using facades):
$user = app('auth')->guard()->user()
This is probably equivalent to the facade call:
$user = Auth::guard()->user()
Hope this helps..
Kind regards,
PS. To figure what class is returned when not using facades, I find myself doing eg. a echo get_class(app('auth'))
to get the class and tell my IDE what kind of variable $auth
is..
Snippet from one of my Lumen controllers
$auth = app('auth');
// echo get_class($auth); to get class
// -> \Illuminate\Auth\AuthManager in this case
/* @var $auth \Illuminate\Auth\AuthManager */
$guard = $auth->guard();
// idem as above
/* @var $guard \Illuminate\Auth\RequestGuard */
$user = $guard->user();
/* @var $auth \App\Auth\User */
// this tells Webstorm/Netbeans/.. to consider $auth an instance of AuthManager and enables autocompletion of class methods
Upvotes: 3