FastAPI JSON Web Token(JWT) 디코딩 함수 구현 - 49

2023. 1. 8. 01:39·Develop/FastAPI
728x90
SMALL
테스트는 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
'Develop/FastAPI' 카테고리의 다른 글
  • FastAPI Postman 설치 / 사용법(GET kakao 검색) - 51
  • FastAPI Custom HTTPException for Auth - 50
  • FastAPI JSON Web Token(JWT) 생성 - 48
  • FastAPI 사용자 인증 - 47
동석해요
동석해요
공부하고 싶은게 많은, 사소한 IT 지식들 공유
    250x250
  • 동석해요
    개발로 자기계발
    동석해요
  • 전체
    오늘
    어제
    • 분류 전체보기 (226)
      • Develop (126)
        • 기초지식 (12)
        • FastAPI (102)
        • Django (11)
      • Database & Data (62)
        • 기초지식 (16)
        • MySQL (29)
        • PostgreSQL (8)
        • 데이터 분석 (9)
      • 인공지능 (11)
        • PyTorch (9)
      • Cloud (8)
        • AWS (4)
        • GCP (2)
      • 버그처리 (14)
      • 회고 & 일상 (5)
  • 인기 글

  • 최근 글

  • 최근 댓글

  • hELLO· Designed By정상우.v4.10.3
동석해요
FastAPI JSON Web Token(JWT) 디코딩 함수 구현 - 49
상단으로

티스토리툴바