Develop/FastAPI
FastAPI JSON Web Token(JWT) 디코딩 함수 구현 - 49
김잠봉
2023. 1. 8. 01:39
728x90
테스트는 x(나중에 다시) 함수 구현만
1. 라이브러리 import
#JWTError 삽입
from jose import jwt, JWTError
2. 비동기 함수 구현
async def get_current_user(token: str = Depends(oauth2_bearer)):
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
username: str = payload.get("sub")
user_id: int = payload.get("id")
if username is None or user_id is None:
raise HTTPException(status_code=404, detail="User not found")
return {"username": username, "id": user_id}
except JWTError:
return HTTPException(status_code=404, detail="User not found")
728x90
SMALL