Spaces:
Running
Running
Ahmed Mostafa commited on
Commit ·
d580433
1
Parent(s): 4539280
fix log
Browse files- src/auth/dependencies.py +14 -10
src/auth/dependencies.py
CHANGED
|
@@ -30,6 +30,10 @@ async def get_current_user(
|
|
| 30 |
db = get_firebase_db()
|
| 31 |
firebase_error = None
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
# 1. Try Firebase Verification
|
| 34 |
try:
|
| 35 |
firebase_result = verify_token(token)
|
|
@@ -40,11 +44,11 @@ async def get_current_user(
|
|
| 40 |
uid = firebase_payload.get("uid")
|
| 41 |
email = firebase_payload.get("email")
|
| 42 |
if not uid:
|
| 43 |
-
logger.error("Firebase payload missing 'uid'")
|
| 44 |
-
raise HTTPException(status_code=401, detail="Invalid Firebase token payload")
|
| 45 |
|
| 46 |
if db is None:
|
| 47 |
-
logger.warning(f"Firestore not available, returning transient user for {email}")
|
| 48 |
return User(id=uid, email=email or "unknown@example.com", username=firebase_payload.get("name", uid), role="user")
|
| 49 |
|
| 50 |
# Retrieve from Firestore
|
|
@@ -58,7 +62,7 @@ async def get_current_user(
|
|
| 58 |
user_data.setdefault("role", user_data.get("role", "user"))
|
| 59 |
return User(**user_data)
|
| 60 |
else:
|
| 61 |
-
logger.info(f"New Firebase user detected: {email or uid}")
|
| 62 |
return User(
|
| 63 |
id=uid,
|
| 64 |
email=email or "unknown@example.com",
|
|
@@ -69,7 +73,7 @@ async def get_current_user(
|
|
| 69 |
except HTTPException:
|
| 70 |
raise
|
| 71 |
except Exception as e:
|
| 72 |
-
logger.error(f"Unexpected error in Firebase auth path: {str(e)}")
|
| 73 |
firebase_error = str(e)
|
| 74 |
|
| 75 |
# 2. Fallback to Custom JWT Decoding
|
|
@@ -78,7 +82,7 @@ async def get_current_user(
|
|
| 78 |
if payload:
|
| 79 |
username: Optional[str] = payload.get("sub")
|
| 80 |
if not username:
|
| 81 |
-
raise HTTPException(status_code=401, detail="Token missing subject claim")
|
| 82 |
|
| 83 |
if db is None:
|
| 84 |
return User(id="mock_id", email="mock@example.com", username=username, password_hash="mock", role="user")
|
|
@@ -92,15 +96,15 @@ async def get_current_user(
|
|
| 92 |
user_data["id"] = user_doc.id
|
| 93 |
return User(**user_data)
|
| 94 |
else:
|
| 95 |
-
logger.error(f"User {username} not found in database")
|
| 96 |
-
raise HTTPException(status_code=401, detail="User account not found")
|
| 97 |
except HTTPException:
|
| 98 |
raise
|
| 99 |
except Exception as e:
|
| 100 |
-
logger.error(f"Unexpected error in custom JWT auth path: {str(e)}")
|
| 101 |
|
| 102 |
# If both failed, then it's a 401
|
| 103 |
-
error_detail = "Could not validate credentials"
|
| 104 |
if firebase_error:
|
| 105 |
error_detail += f": {firebase_error}"
|
| 106 |
|
|
|
|
| 30 |
db = get_firebase_db()
|
| 31 |
firebase_error = None
|
| 32 |
|
| 33 |
+
# Log token prefix for debugging
|
| 34 |
+
token_prefix = token[:10] if token else "None"
|
| 35 |
+
logger.info(f"Received token starting with: {token_prefix}...")
|
| 36 |
+
|
| 37 |
# 1. Try Firebase Verification
|
| 38 |
try:
|
| 39 |
firebase_result = verify_token(token)
|
|
|
|
| 44 |
uid = firebase_payload.get("uid")
|
| 45 |
email = firebase_payload.get("email")
|
| 46 |
if not uid:
|
| 47 |
+
logger.error("Firebase payload missing 'uid' [CP1]")
|
| 48 |
+
raise HTTPException(status_code=401, detail="Invalid Firebase token payload [CP1]")
|
| 49 |
|
| 50 |
if db is None:
|
| 51 |
+
logger.warning(f"Firestore not available, returning transient user for {email} [CP2]")
|
| 52 |
return User(id=uid, email=email or "unknown@example.com", username=firebase_payload.get("name", uid), role="user")
|
| 53 |
|
| 54 |
# Retrieve from Firestore
|
|
|
|
| 62 |
user_data.setdefault("role", user_data.get("role", "user"))
|
| 63 |
return User(**user_data)
|
| 64 |
else:
|
| 65 |
+
logger.info(f"New Firebase user detected: {email or uid} [CP3]")
|
| 66 |
return User(
|
| 67 |
id=uid,
|
| 68 |
email=email or "unknown@example.com",
|
|
|
|
| 73 |
except HTTPException:
|
| 74 |
raise
|
| 75 |
except Exception as e:
|
| 76 |
+
logger.error(f"Unexpected error in Firebase auth path: {str(e)} [CP4]")
|
| 77 |
firebase_error = str(e)
|
| 78 |
|
| 79 |
# 2. Fallback to Custom JWT Decoding
|
|
|
|
| 82 |
if payload:
|
| 83 |
username: Optional[str] = payload.get("sub")
|
| 84 |
if not username:
|
| 85 |
+
raise HTTPException(status_code=401, detail="Token missing subject claim [CP5]")
|
| 86 |
|
| 87 |
if db is None:
|
| 88 |
return User(id="mock_id", email="mock@example.com", username=username, password_hash="mock", role="user")
|
|
|
|
| 96 |
user_data["id"] = user_doc.id
|
| 97 |
return User(**user_data)
|
| 98 |
else:
|
| 99 |
+
logger.error(f"User {username} not found in database [CP6]")
|
| 100 |
+
raise HTTPException(status_code=401, detail="User account not found [CP6]")
|
| 101 |
except HTTPException:
|
| 102 |
raise
|
| 103 |
except Exception as e:
|
| 104 |
+
logger.error(f"Unexpected error in custom JWT auth path: {str(e)} [CP7]")
|
| 105 |
|
| 106 |
# If both failed, then it's a 401
|
| 107 |
+
error_detail = "Could not validate credentials [CP8]"
|
| 108 |
if firebase_error:
|
| 109 |
error_detail += f": {firebase_error}"
|
| 110 |
|