vdomah / oc-jwtauth

JWTAuth plugin for October CMS wich provides token based authentication
MIT License
27 stars 20 forks source link

How to retrieve the User session after login #15

Closed chrisvidal closed 6 years ago

chrisvidal commented 6 years ago

Hi, How to retrieve the User session after login? I am building API using this plugin to handle the authentication. How do I get the logged user session? I tried the usual

$user = Auth::getUser();

but it does not work

thanks

vdomah commented 6 years ago

From your question I see you need just User model instance not user's session. To achieve this you need to get user's id from token and then get the model:

$user_id = JWTAuth::authenticate($token)->id;
// or
$user_id = JWTAuth::parseToken()->authenticate()->id;
// then
$model = UserModel::findOrFail($user_id);
chrisvidal commented 6 years ago

ok thanks, I came up with

$token = JWTAuth::parseToken()->getToken();
$user = JWTAuth::authenticate( $token );