Testys commited on
Commit
bb31c03
1 Parent(s): fd26033

Update core/security.py

Browse files
Files changed (1) hide show
  1. core/security.py +15 -13
core/security.py CHANGED
@@ -4,7 +4,6 @@ from fastapi import Depends, HTTPException
4
  from datetime import timedelta, datetime
5
  from jose import JWTError, jwt
6
  from core.config import get_settings
7
- from users.services import get_user_by_email
8
  from sqlalchemy.orm import Session
9
  from core.database import get_db
10
 
@@ -46,28 +45,31 @@ def get_token_payload(token:str):
46
  except JWTError:
47
  return None
48
 
49
- async def get_current_user(token:str = Depends(oauth2scheme), db:Session = Depends(get_db)):
 
 
50
  try:
51
  payload = get_token_payload(token)
52
  email = payload.get("sub")
53
  if email is None:
54
- return HTTPException(status_code=401,
55
- detail="Invalid Token",
56
- headers={"WWW-Authenticate": "Bearer"})
57
  except JWTError:
58
- return HTTPException(status_code=401,
59
- detail="Invalid Token",
60
- headers={"WWW-Authenticate": "Bearer"}
61
- )
62
 
63
  user = get_user_by_email(email, db=db)
64
  if user is None:
65
- return HTTPException(status_code=401,
66
- detail="Invalid Token",
67
- headers={"WWW-Authenticate": "Bearer"}
68
- )
69
  return user
70
 
71
 
72
 
 
73
 
 
4
  from datetime import timedelta, datetime
5
  from jose import JWTError, jwt
6
  from core.config import get_settings
 
7
  from sqlalchemy.orm import Session
8
  from core.database import get_db
9
 
 
45
  except JWTError:
46
  return None
47
 
48
+ async def get_current_user(token: str = Depends(oauth2scheme), db: Session = Depends(get_db)):
49
+ from users.services import get_user_by_email # Local import
50
+
51
  try:
52
  payload = get_token_payload(token)
53
  email = payload.get("sub")
54
  if email is None:
55
+ raise HTTPException(status_code=401,
56
+ detail="Invalid Token",
57
+ headers={"WWW-Authenticate": "Bearer"})
58
  except JWTError:
59
+ raise HTTPException(status_code=401,
60
+ detail="Invalid Token",
61
+ headers={"WWW-Authenticate": "Bearer"}
62
+ )
63
 
64
  user = get_user_by_email(email, db=db)
65
  if user is None:
66
+ raise HTTPException(status_code=401,
67
+ detail="User not found",
68
+ headers={"WWW-Authenticate": "Bearer"}
69
+ )
70
  return user
71
 
72
 
73
 
74
+
75