如何在 jwt-auth 中获取授权用户的 ID?

How to get ID of authorized user in jwt-auth?

如何获取jwt-auth中授权用户的ID? 我尝试通过标准方法获取 id,但它不起作用

使用jwt-auth时,您可以通过解析 JWT 令牌获取用户 ID:

public function getAuthenticatedUser()
{
    try {
        if (! $user = JWTAuth::parseToken()->authenticate()) {
            return response()->json(['user_not_found'], 404);
        }
    } catch (Tymon\JWTAuth\Exceptions\TokenExpiredException $e) {
        return response()->json(['token_expired'], $e->getStatusCode());
    } catch (Tymon\JWTAuth\Exceptions\TokenInvalidException $e) {
        return response()->json(['token_invalid'], $e->getStatusCode());
    } catch (Tymon\JWTAuth\Exceptions\JWTException $e) {
        return response()->json(['token_absent'], $e->getStatusCode());
    }

    $userId = $user->id;

    # code...
}