Ali2206 commited on
Commit
69fa5a7
Β·
verified Β·
1 Parent(s): 43a7dd6

Update core/security.py

Browse files
Files changed (1) hide show
  1. 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
- print("πŸ” Raw token received:", token)
 
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
- print("❌ JWTError while decoding token:", str(e))
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
- print("βœ… Authenticated user:", user["email"])
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