SyedAzlanzar
@fix : unauth error
f785c0a
raw
history blame contribute delete
710 Bytes
from fastapi import Depends, HTTPException, status
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from jose import jwt, JWTError
import os
security = HTTPBearer()
def verify_token(credentials: HTTPAuthorizationCredentials = Depends(security)):
JWT_SECRET = os.getenv("JWT_SECRET_KEY")
ALGORITHM = os.getenv("ALGORITHM")
token = credentials.credentials
try:
payload = jwt.decode(token, JWT_SECRET, algorithms=[ALGORITHM])
return payload
except JWTError as e:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid or expired token",
headers={"WWW-Authenticate": "Bearer"},
)