Laravel JWT Auth get user on Login

后端 未结 9 2476
迷失自我
迷失自我 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:01

    try this (it works fine with laravel 5.6,5.7):

    use Tymon\JWTAuth\Facades\JWTAuth;
    use Tymon\JWTAuth\Exceptions\JWTException;
    
    // code to generate token for user with email testuser@gmail.com
    $user=User::where('email','=','testuser@gmail.com')->first();
    
    if (!$userToken=JWTAuth::fromUser($user)) {
       return response()->json(['error' => 'invalid_credentials'], 401);
    }
    
    return response()->json(compact('userToken'));
    

提交回复
热议问题