Spaces:
Configuration error
Configuration error
Download backend/core/auth.py from rockysreenivasa/MathTutor: direct link, hf CLI and curl.
- Browser
- Download file 1.02 kB
-
https://huggingface.co/spaces/rockysreenivasa/MathTutor/resolve/main/backend/core/auth.py
- Command line
-
hf download hf://spaces/rockysreenivasa/MathTutor/backend/core/auth.py
-
curl -L -o auth.py https://huggingface.co/spaces/rockysreenivasa/MathTutor/resolve/main/backend/core/auth.py
1.02 kB
| from fastapi import Depends, HTTPException, status | |
| from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer | |
| from supabase import create_client | |
| from backend.core.config import settings | |
| bearer_scheme = HTTPBearer() | |
| # Service-role client — used only for JWT verification | |
| _supabase = create_client(settings.supabase_url, settings.supabase_service_role_key) | |
| async def get_current_user( | |
| credentials: HTTPAuthorizationCredentials = Depends(bearer_scheme), | |
| ) -> dict: | |
| """ | |
| Verify the Supabase JWT passed in the Authorization header. | |
| Returns the decoded user payload on success. | |
| """ | |
| token = credentials.credentials | |
| try: | |
| response = _supabase.auth.get_user(token) | |
| if response.user is None: | |
| raise ValueError("No user in token") | |
| return {"id": response.user.id, "email": response.user.email} | |
| except Exception: | |
| raise HTTPException( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| detail="Invalid or expired token", | |
| ) | |