Spaces:
Sleeping
Sleeping
Update api/routes/auth.py
Browse files- api/routes/auth.py +27 -20
api/routes/auth.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from fastapi import APIRouter, HTTPException, Depends, status
|
| 2 |
from fastapi.security import OAuth2PasswordRequestForm
|
| 3 |
from db.mongo import users_collection
|
| 4 |
from core.security import hash_password, verify_password, create_access_token, get_current_user
|
|
@@ -122,28 +122,35 @@ async def login(form_data: OAuth2PasswordRequestForm = Depends()):
|
|
| 122 |
}
|
| 123 |
|
| 124 |
@router.get("/me")
|
| 125 |
-
async def get_me(current_user: dict = Depends(get_current_user)):
|
| 126 |
-
print(f"Headers: {request.headers}")
|
| 127 |
logger.info(f"Fetching user profile for {current_user['email']}")
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
raise HTTPException(
|
| 132 |
-
status_code=status.
|
| 133 |
-
detail="
|
| 134 |
)
|
| 135 |
-
|
| 136 |
-
response = {
|
| 137 |
-
"id": str(user["_id"]),
|
| 138 |
-
"email": user["email"],
|
| 139 |
-
"full_name": user.get("full_name", ""),
|
| 140 |
-
"role": user.get("role", "patient"),
|
| 141 |
-
"specialty": user.get("specialty"),
|
| 142 |
-
"created_at": user.get("created_at"),
|
| 143 |
-
"updated_at": user.get("updated_at")
|
| 144 |
-
}
|
| 145 |
-
logger.info(f"User profile retrieved for {current_user['email']}")
|
| 146 |
-
return response
|
| 147 |
|
| 148 |
# Export the router as 'auth' for api.__init__.py
|
| 149 |
auth = router
|
|
|
|
| 1 |
+
from fastapi import APIRouter, HTTPException, Depends, status, Request
|
| 2 |
from fastapi.security import OAuth2PasswordRequestForm
|
| 3 |
from db.mongo import users_collection
|
| 4 |
from core.security import hash_password, verify_password, create_access_token, get_current_user
|
|
|
|
| 122 |
}
|
| 123 |
|
| 124 |
@router.get("/me")
|
| 125 |
+
async def get_me(request: Request, current_user: dict = Depends(get_current_user)):
|
|
|
|
| 126 |
logger.info(f"Fetching user profile for {current_user['email']}")
|
| 127 |
+
print(f"Headers: {request.headers}")
|
| 128 |
+
try:
|
| 129 |
+
user = await users_collection.find_one({"email": current_user["email"]})
|
| 130 |
+
if not user:
|
| 131 |
+
logger.warning(f"User not found: {current_user['email']}")
|
| 132 |
+
raise HTTPException(
|
| 133 |
+
status_code=status.HTTP_404_NOT_FOUND,
|
| 134 |
+
detail="User not found"
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
response = {
|
| 138 |
+
"id": str(user["_id"]),
|
| 139 |
+
"email": user["email"],
|
| 140 |
+
"full_name": user.get("full_name", ""),
|
| 141 |
+
"role": user.get("role", "patient"),
|
| 142 |
+
"specialty": user.get("specialty"),
|
| 143 |
+
"created_at": user.get("created_at"),
|
| 144 |
+
"updated_at": user.get("updated_at")
|
| 145 |
+
}
|
| 146 |
+
logger.info(f"User profile retrieved for {current_user['email']}")
|
| 147 |
+
return response
|
| 148 |
+
except Exception as e:
|
| 149 |
+
logger.error(f"Database error for user {current_user['email']}: {str(e)}")
|
| 150 |
raise HTTPException(
|
| 151 |
+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 152 |
+
detail=f"Database error: {str(e)}"
|
| 153 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
# Export the router as 'auth' for api.__init__.py
|
| 156 |
auth = router
|