Spaces:
Sleeping
Sleeping
Update auth.py
Browse files
auth.py
CHANGED
@@ -16,15 +16,15 @@ from pymongo import MongoClient
|
|
16 |
import gridfs
|
17 |
|
18 |
from models import User, UserUpdate, Token, LoginResponse
|
|
|
19 |
|
20 |
load_dotenv()
|
21 |
|
22 |
logger = logging.getLogger("uvicorn")
|
23 |
logger.setLevel(logging.INFO)
|
24 |
|
25 |
-
# MongoDB
|
26 |
-
|
27 |
-
client = MongoClient(MONGO_URL)
|
28 |
db = client.users_database
|
29 |
users_collection = db.users
|
30 |
# GridFS instance for storing avatars
|
@@ -32,7 +32,6 @@ fs = gridfs.GridFS(db, collection="avatars")
|
|
32 |
|
33 |
# OAuth2 setup
|
34 |
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
|
35 |
-
|
36 |
router = APIRouter(prefix="/auth", tags=["auth"])
|
37 |
|
38 |
# Password hashing
|
@@ -57,20 +56,18 @@ def create_token(data: dict, expires_delta: timedelta = None) -> str:
|
|
57 |
to_encode = data.copy()
|
58 |
expire = datetime.utcnow() + (expires_delta or timedelta(minutes=15))
|
59 |
to_encode.update({"exp": expire})
|
60 |
-
secret_key = os.getenv("SECRET_KEY")
|
61 |
algorithm = "HS256"
|
62 |
-
return jwt.encode(to_encode,
|
63 |
|
64 |
def create_access_token(email: str) -> str:
|
65 |
-
return create_token({"sub": email}, timedelta(minutes=
|
66 |
|
67 |
def create_refresh_token(email: str) -> str:
|
68 |
-
return create_token({"sub": email}, timedelta(days=
|
69 |
|
70 |
def get_current_user(token: str = Depends(oauth2_scheme)) -> dict:
|
71 |
-
secret_key = os.getenv("SECRET_KEY")
|
72 |
try:
|
73 |
-
payload = jwt.decode(token,
|
74 |
email: str = payload.get("sub")
|
75 |
if not email:
|
76 |
raise HTTPException(status_code=401, detail="Invalid credentials")
|
|
|
16 |
import gridfs
|
17 |
|
18 |
from models import User, UserUpdate, Token, LoginResponse
|
19 |
+
from config import CONNECTION_STRING, SECRET_KEY, ACCESS_TOKEN_EXPIRE_MINUTES, REFRESH_TOKEN_EXPIRE_DAYS
|
20 |
|
21 |
load_dotenv()
|
22 |
|
23 |
logger = logging.getLogger("uvicorn")
|
24 |
logger.setLevel(logging.INFO)
|
25 |
|
26 |
+
# Updated MongoDB initialization: now using CONNECTION_STRING from config.py
|
27 |
+
client = MongoClient(CONNECTION_STRING)
|
|
|
28 |
db = client.users_database
|
29 |
users_collection = db.users
|
30 |
# GridFS instance for storing avatars
|
|
|
32 |
|
33 |
# OAuth2 setup
|
34 |
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
|
|
|
35 |
router = APIRouter(prefix="/auth", tags=["auth"])
|
36 |
|
37 |
# Password hashing
|
|
|
56 |
to_encode = data.copy()
|
57 |
expire = datetime.utcnow() + (expires_delta or timedelta(minutes=15))
|
58 |
to_encode.update({"exp": expire})
|
|
|
59 |
algorithm = "HS256"
|
60 |
+
return jwt.encode(to_encode, SECRET_KEY, algorithm=algorithm)
|
61 |
|
62 |
def create_access_token(email: str) -> str:
|
63 |
+
return create_token({"sub": email}, timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES))
|
64 |
|
65 |
def create_refresh_token(email: str) -> str:
|
66 |
+
return create_token({"sub": email}, timedelta(days=REFRESH_TOKEN_EXPIRE_DAYS))
|
67 |
|
68 |
def get_current_user(token: str = Depends(oauth2_scheme)) -> dict:
|
|
|
69 |
try:
|
70 |
+
payload = jwt.decode(token, SECRET_KEY, algorithms=["HS256"])
|
71 |
email: str = payload.get("sub")
|
72 |
if not email:
|
73 |
raise HTTPException(status_code=401, detail="Invalid credentials")
|