service_man/api/application/usecase/auth/auth_user.py

35 lines
1.3 KiB
Python
Raw Normal View History

2024-04-02 22:33:15 +03:00
from api.application.contracts.auth.auth_request import LoginRequest
from api.application.contracts.auth.auth_response import AuthenticationResponse
from api.application.protocols.password_hasher import PasswordHasher
from api.domain.user.error import UserInvalidCredentialsError
from api.domain.user.repository import UserRepository
class LoginUser:
def __init__(
self,
user_repository: UserRepository,
password_hasher: PasswordHasher,
) -> None:
self.user_repository = user_repository
self.hasher = password_hasher
async def __call__(self, request: LoginRequest) -> AuthenticationResponse:
2024-04-04 13:51:51 +03:00
print("__call__ request", request)
2024-04-02 22:33:15 +03:00
user = await self.user_repository.get_user(filter={"email": request.email})
2024-04-04 13:51:51 +03:00
print("__call__ user from repo", user)
2024-04-02 22:33:15 +03:00
error = UserInvalidCredentialsError("Email or password is incorrect")
if user is None:
2024-04-04 13:51:51 +03:00
print("user is none in LoginUser __call__")
2024-04-02 22:33:15 +03:00
raise error
if not self.hasher.verify_password(request.password, user.hashed_password):
2024-04-04 13:51:51 +03:00
print("wrong pass in LoginUser __call__")
2024-04-02 22:33:15 +03:00
raise error
return AuthenticationResponse(
id=user.id.value,
name=user.name.value,
email=user.email.value,
)