Skip to content

Commit f289db6

Browse files
committed
Modify condiftions and cast to bool the validations
1 parent ea2411b commit f289db6

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

routers/user.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,30 @@
1212

1313
@user_router.post('/users', tags=['Auth'], response_model=User, status_code=status.HTTP_200_OK)
1414
def create_user(user: UserCreate):
15-
check_user_exists(user)
15+
if check_user_exists(user):
16+
return JSONResponse(status_code=status.HTTP_400_BAD_REQUEST, content={"message": "User already exists"})
1617

1718
UserService(db).create_user(user)
1819

1920
return JSONResponse(status_code=status.HTTP_200_OK, content={"message": "User created"})
2021

2122

2223
def check_user_exists(user):
23-
if UserService(db).get_user_by_email(email=user.email):
24-
return JSONResponse(status_code=status.HTTP_400_BAD_REQUEST, content={"message": "User already exists"})
24+
return bool(UserService(db).get_user_by_email(email=user.email))
2525

2626

2727
@user_router.post('/login', tags=['Auth'], status_code=status.HTTP_200_OK)
2828
def login(user: UserCreate):
29-
validate_password(user)
29+
30+
if validates_password(user):
31+
return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED, content={"message": "Unauthorized"})
3032

3133
token: str = create_token(user.dict())
3234

3335
return JSONResponse(status_code=status.HTTP_200_OK, content=token)
3436

3537

36-
def validate_password(user):
38+
def validates_password(user):
3739
user_found = UserService(db).get_user_by_email(email=user.email)
3840

39-
if not (user_found and Auth().verify_password(user.password, user_found.password)):
40-
return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED, content={"message": "Unauthorized"})
41+
return not bool(check_user_exists(user) and Auth().verify_password(user.password, user_found.password))

0 commit comments

Comments
 (0)