Exemplo n.º 1
0
        public async Task <bool> Handle(LoginRequest message, IOutputPort <LoginResponse> outputPort)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(message.Email))
                {
                    throw new ArgumentNullException(nameof(message.Email));
                }

                if (string.IsNullOrWhiteSpace(message.PasswordHash))
                {
                    throw new ArgumentNullException(nameof(message.PasswordHash));
                }

                var user = await _userRepository.GetBy(message.Email);

                if (user == null || await _userRepository.Validate(user.Email, message.PasswordHash) == false)
                {
                    throw new ApplicationException("Invalid email or password.");
                }

                var token = await _jwtFactory.GenerateAuthToken(user.Id, user.Email);

                if (token == null)
                {
                    throw new ArgumentNullException(nameof(token));
                }

                outputPort.Handle(new LoginResponse(token));

                return(true);
            }
            catch (Exception ex)
            {
                outputPort.Handle(new LoginResponse(new[] { new Error("login", ex.Message) }));
                return(false);
            }
        }