40 lines
No EOL
1.2 KiB
Python
40 lines
No EOL
1.2 KiB
Python
from fastapi import Depends, HTTPException, status
|
|
from fastapi.security import OAuth2PasswordBearer
|
|
from typing import Optional
|
|
from supabase import Client
|
|
from app.config import settings
|
|
from supabase import create_client
|
|
|
|
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="auth/login", auto_error=False)
|
|
|
|
|
|
def get_supabase() -> Client:
|
|
supabase_client = create_client(settings.supabase_url, settings.supabase_key)
|
|
return supabase_client
|
|
|
|
# Updated current user dependency
|
|
async def get_user_from_token(
|
|
token: str = Depends(oauth2_scheme),
|
|
supabase: Client = Depends(get_supabase)
|
|
):
|
|
try:
|
|
# Get user from Supabase auth
|
|
return supabase.auth.get_user(token)
|
|
except Exception as e:
|
|
return None
|
|
|
|
def get_current_user_required(user: Optional[dict] = Depends(get_user_from_token)):
|
|
credentials_exception = HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Could not validate credentials",
|
|
headers={"WWW-Authenticate": "Bearer"},
|
|
)
|
|
if not user:
|
|
raise credentials_exception
|
|
|
|
return user
|
|
|
|
def get_current_user_optional(
|
|
user: Optional[dict] = Depends(get_user_from_token)
|
|
) -> Optional[dict]:
|
|
return user |