Spaces:
Running

mzlessons / dependencies.py
testser2's picture
Upload 5 files
dc19c4a verified
Raw History Blame Contribute Delete
1.05 kB
from fastapi import Header, HTTPException, Depends
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from jose import jwt
from typing import Optional
import os
from dotenv import load_dotenv
load_dotenv()
JWT_SECRET = os.getenv("SUPABASE_JWT_SECRET")
security = HTTPBearer()
async def verify_jwt_token(credentials: HTTPAuthorizationCredentials = Depends(security)):
try:
token = credentials.credentials
# Verify the JWT token using your Supabase public key
# You can get the JWT secret from your Supabase dashboard
payload = jwt.decode(
token,
JWT_SECRET,
algorithms=["HS256"],
audience="authenticated"
)
return payload
except jwt.ExpiredSignatureError:
raise HTTPException(
status_code=401,
detail="Token has expired"
)
except jwt.JWTError:
raise HTTPException(
status_code=401,
detail="Invalid token"
)