Hamdy005 commited on
Commit
a7cc0bd
·
1 Parent(s): 86a47ea

fix: fixing current user id logic

Browse files
Files changed (1) hide show
  1. dependencies.py +17 -20
dependencies.py CHANGED
@@ -29,30 +29,27 @@ async def get_current_user(
29
  }
30
  return DEV_USER
31
 
32
- # Real Supabase auth - use anon key client for token verification
33
- if auth_client is not None:
34
- if not token:
35
- raise HTTPException(status.HTTP_401_UNAUTHORIZED, "Not authenticated")
36
  try:
37
- response = auth_client.auth.get_user(token)
 
 
 
 
38
  except Exception:
39
  raise HTTPException(status.HTTP_401_UNAUTHORIZED, "Invalid authentication")
40
- user = getattr(response, "user", None) or response
41
- if not user:
42
- raise HTTPException(status.HTTP_401_UNAUTHORIZED, "Unauthorized")
43
- return user
44
-
45
- # Fallback to service_role client if anon key not available
46
- if not token:
47
- raise HTTPException(status.HTTP_401_UNAUTHORIZED, "Not authenticated")
48
- try:
49
- response = client.auth.get_user(token)
50
- except Exception:
51
- raise HTTPException(status.HTTP_401_UNAUTHORIZED, "Invalid authentication")
52
- user = getattr(response, "user", None) or response
53
- if not user:
54
  raise HTTPException(status.HTTP_401_UNAUTHORIZED, "Unauthorized")
55
- return user
 
 
 
 
 
 
 
 
 
56
 
57
 
58
  async def get_current_user_id(current_user=Depends(get_current_user)) -> str:
 
29
  }
30
  return DEV_USER
31
 
32
+ # If a Bearer token is present, verify it with Supabase
33
+ if token:
 
 
34
  try:
35
+ verify_client = auth_client if auth_client is not None else client
36
+ response = verify_client.auth.get_user(token)
37
+ user = getattr(response, "user", None) or response
38
+ if user:
39
+ return user
40
  except Exception:
41
  raise HTTPException(status.HTTP_401_UNAUTHORIZED, "Invalid authentication")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  raise HTTPException(status.HTTP_401_UNAUTHORIZED, "Unauthorized")
43
+
44
+ # No Bearer token: fall back to x-user-id header (e.g. passed by the frontend session)
45
+ if x_user_id:
46
+ return {
47
+ "id": x_user_id,
48
+ "name": x_user_name or "User",
49
+ "email": x_user_email or f"user{x_user_id}@studymate.ai",
50
+ }
51
+
52
+ raise HTTPException(status.HTTP_401_UNAUTHORIZED, "Not authenticated")
53
 
54
 
55
  async def get_current_user_id(current_user=Depends(get_current_user)) -> str: