protected override async Task Handle(ChangePasswordCommand command, CancellationToken cancellationToken)
 {
     await _handler
     .Run(async() =>
     {
         await _passwordService.ChangeAsync(command.UserId, command.CurrentPassword,
                                            command.NewPassword);
     })
     .OnSuccess(async() =>
     {
         await _mediatRBus.PublishAsync(
             new PasswordChangedDomainEvent(command.Request.Id, command.UserId),
             cancellationToken);
     })
     .OnCustomError(async customException =>
     {
         await _mediatRBus.PublishAsync(
             new ChangePasswordRejectedDomainEvent(command.Request.Id, command.UserId,
                                                   customException.Code, customException.Message), cancellationToken);
     })
     .OnError(async(exception, logger) =>
     {
         logger.Error(exception, $"Error when changing a password for user with id: {command.UserId}.",
                      exception);
         await _mediatRBus.PublishAsync(
             new ChangePasswordRejectedDomainEvent(command.Request.Id, command.UserId, Codes.Error,
                                                   exception.Message), cancellationToken);
     })
     .ExecuteAsync();
 }
 public async Task HandleAsync(ChangePassword command)
 {
     await _handler
     .Run(async() => await _passwordService.ChangeAsync(command.UserId,
                                                        command.CurrentPassword, command.NewPassword))
     .OnSuccess(async() => await _bus.PublishAsync(
                    new PasswordChanged(command.Request.Id, command.UserId)))
     .OnCustomError(async ex => await _bus.PublishAsync(
                        new ChangePasswordRejected(command.Request.Id, command.UserId,
                                                   ex.Code, ex.Message)))
     .OnError(async(ex, logger) =>
     {
         logger.Error(ex, "Error when trying to change password.");
         await _bus.PublishAsync(new ChangePasswordRejected(command.Request.Id, command.UserId,
                                                            OperationCodes.Error, "Error when trying to change password."));
     })
     .ExecuteAsync();
 }
예제 #3
0
        public async Task <IActionResult> Change(string email, string oldPassword, string newPassword)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                var output = await _passwordService.ChangeAsync(new PasswordChangeModel()
                {
                    Email       = email,
                    OldPassword = oldPassword,
                    NewPassword = newPassword
                });;

                return(Ok(output));
            }
            catch (Exception e)
            {
                _log.LogError(e, "Change Exception");
                return(StatusCode(500));
            }
        }