2016-09-10 65 views

回答

4

您可以通過解析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... 
} 
相關問題