Ba7ath-Project commited on
Commit
b60b795
·
1 Parent(s): 93d0d78

Fix: Add DB-backed admin authorization fallback to bypass JWT caching

Browse files
Files changed (1) hide show
  1. app/services/auth_service.py +14 -0
app/services/auth_service.py CHANGED
@@ -146,6 +146,20 @@ async def get_current_user(credentials: HTTPAuthorizationCredentials = Depends(s
146
  # Check both app_metadata and user_metadata for is_admin
147
  is_admin = app_metadata.get("is_admin", False) or user_metadata.get("is_admin", False)
148
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
  print(f"DEBUG AUTH: User {payload.get('email')} - is_admin from JWT: {is_admin}")
150
  if not is_admin:
151
  print(f"DEBUG AUTH: Payload metadata: app={app_metadata}, user={user_metadata}")
 
146
  # Check both app_metadata and user_metadata for is_admin
147
  is_admin = app_metadata.get("is_admin", False) or user_metadata.get("is_admin", False)
148
 
149
+ # FALLBACK: Si le JWT est en retard (cache GoTrue), on vérifie la table public.users
150
+ if not is_admin:
151
+ try:
152
+ from app.core.supabase_client import get_admin_client
153
+ admin_client = get_admin_client()
154
+ db_res = admin_client.table("users").select("is_admin").eq("auth_user_id", user_id).execute()
155
+ if db_res.data and db_res.data[0].get("is_admin"):
156
+ is_admin = True
157
+ print(f"DEBUG AUTH: Fallback DB réussi pour {payload.get('email')}")
158
+ except Exception as e:
159
+ print(f"WARNING: Echec du fallback DB auth : {e}")
160
+
161
+ print(f"DEBUG AUTH: User {payload.get('email')} - is_admin final: {is_admin}")
162
+
163
  print(f"DEBUG AUTH: User {payload.get('email')} - is_admin from JWT: {is_admin}")
164
  if not is_admin:
165
  print(f"DEBUG AUTH: Payload metadata: app={app_metadata}, user={user_metadata}")