35 lines
1.3 KiB
Python
35 lines
1.3 KiB
Python
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:
|
|
print("__call__ request", request)
|
|
user = await self.user_repository.get_user(filter={"email": request.email})
|
|
print("__call__ user from repo", user)
|
|
error = UserInvalidCredentialsError("Email or password is incorrect")
|
|
if user is None:
|
|
print("user is none in LoginUser __call__")
|
|
raise error
|
|
|
|
if not self.hasher.verify_password(request.password, user.hashed_password):
|
|
print("wrong pass in LoginUser __call__")
|
|
raise error
|
|
|
|
return AuthenticationResponse(
|
|
id=user.id.value,
|
|
name=user.name.value,
|
|
email=user.email.value,
|
|
)
|