Skip to content

Commit

Permalink
update get_current_user return type (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lolomgrofl committed Jul 17, 2023
1 parent a21e5f2 commit b7c6127
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
4 changes: 2 additions & 2 deletions {{cookiecutter.repo_name}}/app/routers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ async def token(


@router.get("/login", status_code=status.HTTP_200_OK)
async def login(current_user=Depends(UserService.get_current_user)):
return current_user
async def login(current_user=Depends(UserService.get_current_user)) -> UserOut:
return UserOut.model_validate(current_user)


@router.get("/get_by_id/{user_id}", status_code=status.HTTP_200_OK)
Expand Down
31 changes: 17 additions & 14 deletions {{cookiecutter.repo_name}}/app/services/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from app.daos import user
from app.db import get_session
from app.models.user import User as UserModel
from app.schemas.token import Token, TokenData
from app.schemas.user import ChangePasswordIn, UserIn, UserOut
from app.services.utils import UtilsService, oauth2_scheme
Expand All @@ -35,16 +36,16 @@ async def register_user(user_data: UserIn, session: AsyncSession):
)

@staticmethod
async def authenticate_user(session: AsyncSession, email: str, password: str):
async def authenticate_user(
session: AsyncSession, email: str, password: str
) -> UserModel | bool:
_user = await user.UserDao(session).get_by_email(email)
if not _user:
return False
if not UtilsService.verify_password(password, _user.password):
if not _user or not UtilsService.verify_password(password, _user.password):
return False
return _user

@staticmethod
async def user_email_exists(session: AsyncSession, email: str):
async def user_email_exists(session: AsyncSession, email: str) -> UserModel | None:
_user = await user.UserDao(session).get_by_email(email)
return _user if _user else None

Expand Down Expand Up @@ -75,7 +76,7 @@ async def login(
async def get_current_user(
session: AsyncSession = Depends(get_session),
token: str = Depends(oauth2_scheme),
) -> UserOut:
) -> UserModel:
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
Expand All @@ -94,10 +95,10 @@ async def get_current_user(
_user = await user.UserDao(session).get_by_email(email=token_data.email)
if not _user:
raise credentials_exception
return UserOut.model_validate(_user)
return _user

@staticmethod
async def get_all_users(session: AsyncSession):
async def get_all_users(session: AsyncSession) -> list[UserOut]:
all_users = await user.UserDao(session).get_all()
return [UserOut.model_validate(_user) for _user in all_users]

Expand All @@ -112,18 +113,20 @@ async def delete_all_users(session: AsyncSession):
@staticmethod
async def change_password(
password_data: ChangePasswordIn,
current_user,
current_user: UserModel,
session: AsyncSession = Depends(get_session),
):
_user = await user.UserDao(session).get_by_id(current_user.id)
logger.info(f"Current user: {current_user}")
if not UtilsService.verify_password(password_data.old_password, _user.password):
if not UtilsService.verify_password(
password_data.old_password, current_user.password
):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Incorrect old password!!!",
)
_user.password = UtilsService.get_password_hash(password_data.new_password)
session.add(_user)
current_user.password = UtilsService.get_password_hash(
password_data.new_password
)
session.add(current_user)
await session.commit()
return JSONResponse(
content={"message": "Password updated successfully!!!"},
Expand Down

0 comments on commit b7c6127

Please sign in to comment.