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
'Develop > FastAPI' 카테고리의 다른 글
FastAPI Postman 설치 / 사용법(GET kakao 검색) - 51 (2) | 2023.01.09 |
---|---|
FastAPI Custom HTTPException for Auth - 50 (0) | 2023.01.08 |
FastAPI JSON Web Token(JWT) 생성 - 48 (0) | 2023.01.08 |
FastAPI 사용자 인증 - 47 (0) | 2023.01.07 |
FastAPI 데이터베이스에 유저 데이터 저장하기 - 46 (0) | 2023.01.07 |