31 lines
1.1 KiB
Python
31 lines
1.1 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:
|
||
|
user = await self.user_repository.get_user(filter={"email": request.email})
|
||
|
error = UserInvalidCredentialsError("Email or password is incorrect")
|
||
|
if user is None:
|
||
|
raise error
|
||
|
|
||
|
if not self.hasher.verify_password(request.password, user.hashed_password):
|
||
|
raise error
|
||
|
|
||
|
return AuthenticationResponse(
|
||
|
id=user.id.value,
|
||
|
name=user.name.value,
|
||
|
email=user.email.value,
|
||
|
)
|