Laravel JWT Auth get user on Login

后端 未结 9 2479
迷失自我
迷失自我 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条回答
  •  梦毁少年i
    2021-02-01 21:58

    You don't need Auth::user();

    You can use the toUser method of JWTAuth. Just pass the token as parameter and you will get back the user info:

    $user = JWTAuth::toUser($token);
    
    return response()->json(compact('token', 'user'));
    

    For more info, this is the toUser method:

    /**
     * Find a user using the user identifier in the subject claim.
     *
     * @param bool|string $token
     *
     * @return mixed
     */
    
    public function toUser($token = false)
    {
        $payload = $this->getPayload($token);
    
        if (! $user = $this->user->getBy($this->identifier, $payload['sub'])) {
            return false;
        }
    
        return $user;
    }
    

提交回复
热议问题