Spaces:
Sleeping
Sleeping
| 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 |