Laravel JWT Auth get user on Login

后端 未结 9 2462
迷失自我
迷失自我 2021-02-01 21:16

Is it possible with https://github.com/tymondesigns/jwt-auth to get the current user? Because right now I can only generate a token (when a user sign in).

9条回答
  •  盖世英雄少女心
    2021-02-01 22:13

    You can also use JWTAuth::user() method.

    The user() method call is returned in the toUser() method, which itself is an alias for authenticate() method which authenticates a user via a token. If the user is already authenticated, there is no need to authenticate them again (which toUser() does), instead user() method can be used to get the authenticated user.

    // $token = JWTAuth::attempt($credentials) was successful...
    
    $user = JWTAuth::user();
    
    return response()->json(compact('token', 'user'));
    

提交回复
热议问题