Spaces:
Running
Running
| from supabase import create_client, Client | |
| from .config import settings | |
| from fastapi import Request, HTTPException | |
| import logging | |
| logger = logging.getLogger(__name__) | |
| # Initialize Supabase Client | |
| supabase: Client = create_client(settings.supabase_url, settings.supabase_anon_key) | |
| supabase_admin: Client = create_client(settings.supabase_url, settings.supabase_service_role_key) | |
| def get_supabase_client(): | |
| """Dependency to get Supabase client""" | |
| return supabase | |
| async def get_current_user(request: Request): | |
| """Dependency to verify Supabase token and return user data""" | |
| auth_header = request.headers.get("Authorization") | |
| if not auth_header or not auth_header.startswith("Bearer "): | |
| raise HTTPException(status_code=401, detail="Missing or invalid authorization header") | |
| token = auth_header.split(" ")[1] | |
| try: | |
| # Use get_user to verify token and fetch user info | |
| response = supabase.auth.get_user(token) | |
| if not response or not response.user: | |
| raise HTTPException(status_code=401, detail="Invalid Supabase token") | |
| user = response.user | |
| # We return a dict that matches what the application expects | |
| user_dict = { | |
| "uid": user.id, | |
| "id": user.id, | |
| "email": user.email, | |
| "user_metadata": user.user_metadata, | |
| "app_metadata": user.app_metadata | |
| } | |
| return user_dict | |
| except Exception as e: | |
| logger.error(f"Supabase Auth Error: {e}") | |
| raise HTTPException(status_code=401, detail=f"Invalid Supabase token: {str(e)}") | |