Ali2206 commited on
Commit
8795c52
Β·
verified Β·
1 Parent(s): 397123d

Update core/security.py

Browse files
Files changed (1) hide show
  1. 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
- logger.info(f"πŸ” Raw token received: {token}")
 
 
 
 
 
36
  try:
37
  payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
38
- email: str = payload.get("sub")
39
- if email is None:
40
- logger.error("Invalid token: subject missing")
41
- raise HTTPException(status_code=401, detail="Invalid token: subject missing")
 
42
  except JWTError as e:
43
- logger.error(f"❌ JWTError while decoding token: {str(e)}")
44
  raise HTTPException(status_code=401, detail="Could not validate token")
45
-
46
- try:
47
- user = await users_collection.find_one({"email": email})
48
- if not user:
49
- logger.error(f"User not found: {email}")
50
- raise HTTPException(status_code=404, detail="User not found")
51
- logger.info(f"βœ… Authenticated user: {user['email']}")
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