Spaces:
Sleeping
Sleeping
Update core/security.py
Browse files- core/security.py +8 -5
core/security.py
CHANGED
|
@@ -28,21 +28,24 @@ def create_access_token(data: dict, expires_delta: timedelta = None):
|
|
| 28 |
to_encode.update({"exp": expire})
|
| 29 |
return jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
|
| 30 |
|
| 31 |
-
# Get the current user from the JWT token
|
| 32 |
async def get_current_user(token: str = Depends(oauth2_scheme)):
|
| 33 |
-
|
|
|
|
| 34 |
try:
|
| 35 |
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
|
|
|
| 36 |
email: str = payload.get("sub")
|
| 37 |
if email is None:
|
|
|
|
| 38 |
raise HTTPException(status_code=401, detail="Invalid token: subject missing")
|
| 39 |
except JWTError as e:
|
| 40 |
-
|
| 41 |
raise HTTPException(status_code=401, detail="Could not validate token")
|
| 42 |
|
| 43 |
user = await users_collection.find_one({"email": email})
|
| 44 |
if not user:
|
|
|
|
| 45 |
raise HTTPException(status_code=404, detail="User not found")
|
| 46 |
|
| 47 |
-
|
| 48 |
-
return user
|
|
|
|
| 28 |
to_encode.update({"exp": expire})
|
| 29 |
return jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
|
| 30 |
|
|
|
|
| 31 |
async def get_current_user(token: str = Depends(oauth2_scheme)):
|
| 32 |
+
logger.info(f"π Authentication attempt with token: {token[:15]}...") # Log first part of token
|
| 33 |
+
|
| 34 |
try:
|
| 35 |
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
| 36 |
+
logger.info(f"π Token payload: {payload}")
|
| 37 |
email: str = payload.get("sub")
|
| 38 |
if email is None:
|
| 39 |
+
logger.error("β Invalid token: subject missing")
|
| 40 |
raise HTTPException(status_code=401, detail="Invalid token: subject missing")
|
| 41 |
except JWTError as e:
|
| 42 |
+
logger.error(f"β JWTError while decoding token: {str(e)}")
|
| 43 |
raise HTTPException(status_code=401, detail="Could not validate token")
|
| 44 |
|
| 45 |
user = await users_collection.find_one({"email": email})
|
| 46 |
if not user:
|
| 47 |
+
logger.error(f"β User not found for email: {email}")
|
| 48 |
raise HTTPException(status_code=404, detail="User not found")
|
| 49 |
|
| 50 |
+
logger.info(f"β
Authenticated user: {user['email']}")
|
| 51 |
+
return user
|