Spaces:
Sleeping
Sleeping
Hussien Haider commited on
Commit ·
dacb98a
1
Parent(s): efbf2ba
- app/api/auth.py +28 -4
app/api/auth.py
CHANGED
|
@@ -32,20 +32,44 @@ async def handle_auth(request: Request, req: AuthRequest):
|
|
| 32 |
|
| 33 |
# Extract user_id from various possible fields in the response
|
| 34 |
user_id = None
|
|
|
|
|
|
|
| 35 |
if "user_id" in data:
|
| 36 |
user_id = data["user_id"]
|
| 37 |
elif "id" in data:
|
| 38 |
user_id = data["id"]
|
| 39 |
-
elif "user" in data and isinstance(data["user"], dict)
|
| 40 |
-
user_id = data["user"]
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
if user_id:
|
| 45 |
data["user_id"] = user_id
|
| 46 |
log.info("[auth] User logged in, user_id=%s", user_id)
|
| 47 |
else:
|
| 48 |
log.warning("[auth] No user_id in auth response - keys: %s", list(data.keys()))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
# Log the response structure (without secrets) to help debug token issues
|
| 51 |
top_keys = list(data.keys()) if isinstance(data, dict) else type(data).__name__
|
|
|
|
| 32 |
|
| 33 |
# Extract user_id from various possible fields in the response
|
| 34 |
user_id = None
|
| 35 |
+
|
| 36 |
+
# Check for common user identification fields
|
| 37 |
if "user_id" in data:
|
| 38 |
user_id = data["user_id"]
|
| 39 |
elif "id" in data:
|
| 40 |
user_id = data["id"]
|
| 41 |
+
elif "user" in data and isinstance(data["user"], dict):
|
| 42 |
+
user_id = data["user"].get("id") or data["user"].get("user_id") or data["user"].get("sub")
|
| 43 |
+
|
| 44 |
+
# Also check for user_id in JWT token if present in response
|
| 45 |
+
if not user_id and "access_token" in data:
|
| 46 |
+
try:
|
| 47 |
+
import jwt
|
| 48 |
+
token_data = jwt.decode(
|
| 49 |
+
data["access_token"],
|
| 50 |
+
options={"verify_signature": False, "verify_aud": False},
|
| 51 |
+
algorithms=["HS256"]
|
| 52 |
+
)
|
| 53 |
+
user_id = token_data.get("sub")
|
| 54 |
+
except Exception:
|
| 55 |
+
pass
|
| 56 |
+
|
| 57 |
+
# Last resort: check if response is just a user object
|
| 58 |
+
if not user_id and len(data) == 1 and isinstance(list(data.values())[0], dict):
|
| 59 |
+
possible_user = list(data.values())[0]
|
| 60 |
+
user_id = possible_user.get("id") or possible_user.get("user_id") or possible_user.get("sub")
|
| 61 |
|
| 62 |
if user_id:
|
| 63 |
data["user_id"] = user_id
|
| 64 |
log.info("[auth] User logged in, user_id=%s", user_id)
|
| 65 |
else:
|
| 66 |
log.warning("[auth] No user_id in auth response - keys: %s", list(data.keys()))
|
| 67 |
+
# For debugging: log the full response (masked)
|
| 68 |
+
import json
|
| 69 |
+
masked_data = json.dumps(data)
|
| 70 |
+
if len(masked_data) > 500:
|
| 71 |
+
masked_data = masked_data[:500] + "..."
|
| 72 |
+
log.warning("[auth] Full response (masked): %s", masked_data)
|
| 73 |
|
| 74 |
# Log the response structure (without secrets) to help debug token issues
|
| 75 |
top_keys = list(data.keys()) if isinstance(data, dict) else type(data).__name__
|