PensionBot / auth.py
ChAbhishek28's picture
Deploy clean Voice Bot backend to HF Spaces
cf02b2b
raw
history blame contribute delete
825 Bytes
import jwt
from fastapi import HTTPException, status, Header
from jwt import PyJWTError
from dotenv import load_dotenv
import os
load_dotenv()
SUPABASE_JWT_SECRET = os.getenv("SUPABASE_JWT_SECRET")
def verify_token(token: str):
try:
payload = jwt.decode(token, SUPABASE_JWT_SECRET, algorithms=["HS256"], audience="authenticated")
return payload
except PyJWTError:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid authentication credentials",
)
def get_current_user(authorization: str = Header(...)):
if not authorization.startswith("Bearer "):
raise HTTPException(status_code=401, detail="Invalid Authorization header")
token = authorization.split(" ")[1]
payload = verify_token(token)
return payload