Spaces:
Sleeping
Sleeping
Update core/security.py
Browse files- core/security.py +19 -17
core/security.py
CHANGED
|
@@ -32,24 +32,26 @@ def create_access_token(data: dict, expires_delta: timedelta = None):
|
|
| 32 |
|
| 33 |
# Get the current user from the JWT token
|
| 34 |
async def get_current_user(token: str = Depends(oauth2_scheme)):
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
try:
|
| 37 |
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
| 42 |
except JWTError as e:
|
| 43 |
-
|
| 44 |
raise HTTPException(status_code=401, detail="Could not validate token")
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
return user
|
| 53 |
-
except Exception as e:
|
| 54 |
-
logger.error(f"β MongoDB error: {str(e)}")
|
| 55 |
-
raise HTTPException(status_code=500, detail="Database error")
|
|
|
|
| 32 |
|
| 33 |
# Get the current user from the JWT token
|
| 34 |
async def get_current_user(token: str = Depends(oauth2_scheme)):
|
| 35 |
+
print("π Token received:", token)
|
| 36 |
+
|
| 37 |
+
if not token:
|
| 38 |
+
print("β No token received")
|
| 39 |
+
raise HTTPException(status_code=401, detail="No token provided")
|
| 40 |
+
|
| 41 |
try:
|
| 42 |
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
| 43 |
+
print("π§ Token payload:", payload)
|
| 44 |
+
|
| 45 |
+
email = payload.get("sub")
|
| 46 |
+
if not email:
|
| 47 |
+
raise HTTPException(status_code=401, detail="Invalid token: missing subject")
|
| 48 |
except JWTError as e:
|
| 49 |
+
print("β JWT decode error:", str(e))
|
| 50 |
raise HTTPException(status_code=401, detail="Could not validate token")
|
| 51 |
+
|
| 52 |
+
user = await users_collection.find_one({"email": email})
|
| 53 |
+
if not user:
|
| 54 |
+
raise HTTPException(status_code=404, detail="User not found")
|
| 55 |
+
|
| 56 |
+
print("β
Authenticated user:", user["email"])
|
| 57 |
+
return user
|
|
|
|
|
|
|
|
|
|
|
|