chore: Improve error handling and logging in get_current_user_by_jwt function

This commit is contained in:
ogabrielluiz 2024-06-14 08:55:33 -03:00
commit df634de53f

View file

@ -113,7 +113,7 @@ async def get_current_user_by_jwt(
if expires := payload.get("exp", None): if expires := payload.get("exp", None):
expires_datetime = datetime.fromtimestamp(expires, timezone.utc) expires_datetime = datetime.fromtimestamp(expires, timezone.utc)
if datetime.now(timezone.utc) > expires_datetime: if datetime.now(timezone.utc) > expires_datetime:
logger.info("Token expired for user ID: %s", user_id) logger.info("Token expired for user")
raise HTTPException( raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED, status_code=status.HTTP_401_UNAUTHORIZED,
detail="Token has expired.", detail="Token has expired.",
@ -121,14 +121,14 @@ async def get_current_user_by_jwt(
) )
if user_id is None or token_type: if user_id is None or token_type:
logger.info("Invalid token payload: %s", payload) logger.info(f"Invalid token payload. Token type: {token_type}")
raise HTTPException( raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED, status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid token details.", detail="Invalid token details.",
headers={"WWW-Authenticate": "Bearer"}, headers={"WWW-Authenticate": "Bearer"},
) )
except JWTError as e: except JWTError as e:
logger.error("JWT decoding error: %s", str(e)) logger.error(f"JWT decoding error: {e}")
raise HTTPException( raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED, status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials", detail="Could not validate credentials",
@ -137,7 +137,7 @@ async def get_current_user_by_jwt(
user = get_user_by_id(db, user_id) user = get_user_by_id(db, user_id)
if user is None or not user.is_active: if user is None or not user.is_active:
logger.info("User not found or inactive: %s", user_id) logger.info("User not found or inactive.")
raise HTTPException( raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED, status_code=status.HTTP_401_UNAUTHORIZED,
detail="User not found or is inactive.", detail="User not found or is inactive.",