File size: 1,603 Bytes
911fd07
dae1f89
 
911fd07
dae1f89
911fd07
dae1f89
911fd07
 
 
dae1f89
911fd07
 
 
dae1f89
911fd07
 
dae1f89
 
 
 
 
 
911fd07
 
 
 
dae1f89
911fd07
 
 
 
 
 
 
 
 
 
dae1f89
911fd07
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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)}")