Spaces:
Running
Running
File size: 1,774 Bytes
1ef8c5a | 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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | """
Supabase singleton clients.
Two clients:
get_anon_client() — anon key, used for user-facing auth (sign in/out)
get_service_client() — service_role key, used server-side for DB + Storage
(bypasses RLS, never expose to the browser)
get_owner_user_id() — returns the single owner user's UUID (single-user app).
Cached after first call.
"""
import os
SUPABASE_URL = os.getenv("SUPABASE_URL", "")
SUPABASE_ANON_KEY = os.getenv("SUPABASE_ANON_KEY", "")
SUPABASE_SERVICE_ROLE_KEY = os.getenv("SUPABASE_SERVICE_ROLE_KEY", "")
_anon_client = None
_service_client = None
_owner_user_id = None # cached after first admin lookup
def is_configured() -> bool:
return bool(SUPABASE_URL and SUPABASE_ANON_KEY)
def get_anon_client():
global _anon_client
if _anon_client is None:
from supabase import create_client
_anon_client = create_client(SUPABASE_URL, SUPABASE_ANON_KEY)
return _anon_client
def get_service_client():
global _service_client
if _service_client is None:
from supabase import create_client
_service_client = create_client(SUPABASE_URL, SUPABASE_SERVICE_ROLE_KEY)
return _service_client
def get_owner_user_id() -> str | None:
"""Return the single owner user's UUID. Cached after first successful lookup."""
global _owner_user_id
if _owner_user_id:
return _owner_user_id
try:
sb = get_service_client()
result = sb.auth.admin.list_users()
users = result if isinstance(result, list) else getattr(result, "users", [])
if users:
_owner_user_id = users[0].id
return _owner_user_id
except Exception:
pass
return None
|