Spaces:
Runtime error
Runtime error
Commit
·
8d71681
1
Parent(s):
d627b5f
fix
Browse files- dependencies.py +1 -1
- routers/user.py +3 -3
- services/auth_service.py +3 -3
- services/user_service.py +4 -4
dependencies.py
CHANGED
@@ -266,7 +266,7 @@ async def get_current_user(
|
|
266 |
user_data: Optional[dict] = None # Khởi tạo để tránh UnboundLocalError
|
267 |
try:
|
268 |
logger.info(f"GET_CURRENT_USER: Đang tìm user trong DB: {email.lower()}") # email đã được validate là str
|
269 |
-
user_data = mongo_db.users.find_one({"email": email.lower()}, {"password": 0, "_id": 0})
|
270 |
# print(user_data) # Bỏ print trong production
|
271 |
|
272 |
if user_data is None:
|
|
|
266 |
user_data: Optional[dict] = None # Khởi tạo để tránh UnboundLocalError
|
267 |
try:
|
268 |
logger.info(f"GET_CURRENT_USER: Đang tìm user trong DB: {email.lower()}") # email đã được validate là str
|
269 |
+
user_data = await mongo_db.users.find_one({"email": email.lower()}, {"password": 0, "_id": 0})
|
270 |
# print(user_data) # Bỏ print trong production
|
271 |
|
272 |
if user_data is None:
|
routers/user.py
CHANGED
@@ -304,7 +304,7 @@ async def auth_google_callback(request: Request):
|
|
304 |
|
305 |
|
306 |
# Kiểm tra xem user đã tồn tại trong DB chưa
|
307 |
-
db_user = mongo_db.users.find_one({"email": user_email})
|
308 |
|
309 |
if not db_user:
|
310 |
placeholder_password = f"google-oauth2|{uuid.uuid4()}"
|
@@ -324,7 +324,7 @@ async def auth_google_callback(request: Request):
|
|
324 |
})
|
325 |
|
326 |
# Lấy lại user vừa tạo để đảm bảo có _id và các trường khác
|
327 |
-
db_user = mongo_db.users.find_one({"email": user_email})
|
328 |
if not db_user: # Kiểm tra lại sau khi insert
|
329 |
raise HTTPException(status_code=500, detail="Could not create and retrieve new user account.")
|
330 |
|
@@ -408,7 +408,7 @@ async def exchange_google_code_for_token(request: Request,response: Response, co
|
|
408 |
path="/api/user/refresh-token",
|
409 |
)
|
410 |
|
411 |
-
user_info =mongo_db.users.find_one({"email": user_email})
|
412 |
user = {
|
413 |
"email": user_info.get("email"),
|
414 |
"username": user_info.get("username"),
|
|
|
304 |
|
305 |
|
306 |
# Kiểm tra xem user đã tồn tại trong DB chưa
|
307 |
+
db_user = await mongo_db.users.find_one({"email": user_email})
|
308 |
|
309 |
if not db_user:
|
310 |
placeholder_password = f"google-oauth2|{uuid.uuid4()}"
|
|
|
324 |
})
|
325 |
|
326 |
# Lấy lại user vừa tạo để đảm bảo có _id và các trường khác
|
327 |
+
db_user = await mongo_db.users.find_one({"email": user_email})
|
328 |
if not db_user: # Kiểm tra lại sau khi insert
|
329 |
raise HTTPException(status_code=500, detail="Could not create and retrieve new user account.")
|
330 |
|
|
|
408 |
path="/api/user/refresh-token",
|
409 |
)
|
410 |
|
411 |
+
user_info =await mongo_db.users.find_one({"email": user_email})
|
412 |
user = {
|
413 |
"email": user_info.get("email"),
|
414 |
"username": user_info.get("username"),
|
services/auth_service.py
CHANGED
@@ -31,7 +31,7 @@ async def register_user(user: RegisterRequest):
|
|
31 |
|
32 |
try:
|
33 |
# Kiểm tra username đã tồn tại chưa
|
34 |
-
existing_user =mongo_db.users.find_one({"email": user.email})
|
35 |
if existing_user:
|
36 |
raise HTTPException(status_code=400, detail="Tài khoản đã tồn tại.")
|
37 |
|
@@ -54,7 +54,7 @@ async def register_user(user: RegisterRequest):
|
|
54 |
raise HTTPException(status_code=e.status_code, detail=e.detail)
|
55 |
# Hàm xác thực đăng nhập
|
56 |
async def authenticate_user(request: LoginRequest):
|
57 |
-
user =mongo_db.users.find_one({"email": request.email})
|
58 |
if not user:
|
59 |
raise HTTPException(status_code=401, detail="Sai tài khoản hoặc mật khẩu")
|
60 |
|
@@ -110,7 +110,7 @@ async def logout_user(req: Request, credentials: HTTPAuthorizationCredentials =
|
|
110 |
refresh_token = req.cookies.get("refresh_token")
|
111 |
if refresh_token:
|
112 |
# Invalidate refresh token in database
|
113 |
-
user = mongo_db.users.find_one({"refresh_token": refresh_token})
|
114 |
if user:
|
115 |
mongo_db.users.update_one(
|
116 |
{"_id": user["_id"]},
|
|
|
31 |
|
32 |
try:
|
33 |
# Kiểm tra username đã tồn tại chưa
|
34 |
+
existing_user =await mongo_db.users.find_one({"email": user.email})
|
35 |
if existing_user:
|
36 |
raise HTTPException(status_code=400, detail="Tài khoản đã tồn tại.")
|
37 |
|
|
|
54 |
raise HTTPException(status_code=e.status_code, detail=e.detail)
|
55 |
# Hàm xác thực đăng nhập
|
56 |
async def authenticate_user(request: LoginRequest):
|
57 |
+
user = await mongo_db.users.find_one({"email": request.email})
|
58 |
if not user:
|
59 |
raise HTTPException(status_code=401, detail="Sai tài khoản hoặc mật khẩu")
|
60 |
|
|
|
110 |
refresh_token = req.cookies.get("refresh_token")
|
111 |
if refresh_token:
|
112 |
# Invalidate refresh token in database
|
113 |
+
user = await mongo_db.users.find_one({"refresh_token": refresh_token})
|
114 |
if user:
|
115 |
mongo_db.users.update_one(
|
116 |
{"_id": user["_id"]},
|
services/user_service.py
CHANGED
@@ -204,7 +204,7 @@ async def change_password(email: str, current_password: str, new_password: str):
|
|
204 |
bool: True nếu đổi mật khẩu thành công, False nếu không thành công
|
205 |
"""
|
206 |
try:
|
207 |
-
user =mongo_db.users.find_one({"email": email})
|
208 |
if not user:
|
209 |
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Người dùng không tồn tại")
|
210 |
|
@@ -425,7 +425,7 @@ async def verify_login_code(email: str, code: str, res: Response): # Bỏ kiểu
|
|
425 |
HTTPException: Nếu mã không hợp lệ, đã hết hạn hoặc có lỗi hệ thống.
|
426 |
"""
|
427 |
try:
|
428 |
-
user = mongo_db.users.find_one({ # Sử dụng await nếu mongo_db.users là async (ví dụ Motor)
|
429 |
"email": email.lower(),
|
430 |
"login_verification_code": code
|
431 |
})
|
@@ -567,7 +567,7 @@ async def authenticate_user(request: LoginRequest) -> dict:
|
|
567 |
Raises:
|
568 |
HTTPException: Nếu thông tin đăng nhập không hợp lệ.
|
569 |
"""
|
570 |
-
user = mongo_db.users.find_one({"email": request.email.lower()})
|
571 |
if not user or not pwd_context.verify(request.password, user["password"]):
|
572 |
logger.warning(f"Xác thực thất bại cho email: {request.email}")
|
573 |
raise HTTPException(
|
@@ -603,7 +603,7 @@ async def refresh_access_token(req: Request , res: Response ) -> dict:
|
|
603 |
)
|
604 |
|
605 |
# Find user by refresh token in database
|
606 |
-
user = mongo_db.users.find_one({"refresh_token": refresh_token})
|
607 |
|
608 |
|
609 |
if not user:
|
|
|
204 |
bool: True nếu đổi mật khẩu thành công, False nếu không thành công
|
205 |
"""
|
206 |
try:
|
207 |
+
user =await mongo_db.users.find_one({"email": email})
|
208 |
if not user:
|
209 |
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Người dùng không tồn tại")
|
210 |
|
|
|
425 |
HTTPException: Nếu mã không hợp lệ, đã hết hạn hoặc có lỗi hệ thống.
|
426 |
"""
|
427 |
try:
|
428 |
+
user = await mongo_db.users.find_one({ # Sử dụng await nếu mongo_db.users là async (ví dụ Motor)
|
429 |
"email": email.lower(),
|
430 |
"login_verification_code": code
|
431 |
})
|
|
|
567 |
Raises:
|
568 |
HTTPException: Nếu thông tin đăng nhập không hợp lệ.
|
569 |
"""
|
570 |
+
user = await mongo_db.users.find_one({"email": request.email.lower()})
|
571 |
if not user or not pwd_context.verify(request.password, user["password"]):
|
572 |
logger.warning(f"Xác thực thất bại cho email: {request.email}")
|
573 |
raise HTTPException(
|
|
|
603 |
)
|
604 |
|
605 |
# Find user by refresh token in database
|
606 |
+
user =await mongo_db.users.find_one({"refresh_token": refresh_token})
|
607 |
|
608 |
|
609 |
if not user:
|