resetUserPassword method

Future<void> resetUserPassword()

Resets password for the MyUser identified by the provided in recoverAccess identity and ConfirmationCode.

Implementation

Future<void> resetUserPassword() async {
  if (newPassword.error.value != null ||
      repeatPassword.error.value != null ||
      recoveryCode.error.value != null) {
    return;
  }

  repeatPassword.status.value = RxStatus.empty();

  if (newPassword.text.isEmpty) {
    newPassword.error.value = 'err_input_empty'.l10n;
    newPassword.editable.value = true;
    repeatPassword.editable.value = true;
    return;
  }

  if (repeatPassword.text.isEmpty) {
    repeatPassword.error.value = 'err_input_empty'.l10n;
    return;
  }

  if (UserPassword.tryParse(newPassword.text) == null) {
    newPassword.error.value = 'err_incorrect_input'.l10n;
    return;
  }

  if (UserPassword.tryParse(repeatPassword.text) == null) {
    repeatPassword.error.value = 'err_incorrect_input'.l10n;
    return;
  }

  if (newPassword.text != repeatPassword.text) {
    repeatPassword.error.value = 'err_passwords_mismatch'.l10n;
    return;
  }

  newPassword.editable.value = false;
  repeatPassword.editable.value = false;
  newPassword.status.value = RxStatus.loading();
  repeatPassword.status.value = RxStatus.loading();

  try {
    await _authService.updateUserPassword(
      login: _recoveryLogin,
      num: _recoveryNum,
      email: _recoveryEmail,
      phone: _recoveryPhone,
      code: ConfirmationCode(recoveryCode.text.toLowerCase()),
      newPassword: UserPassword(newPassword.text),
    );

    recovered.value = true;
    stage.value = LoginViewStage.signIn;
  } on FormatException {
    repeatPassword.error.value = 'err_incorrect_input'.l10n;
  } on ArgumentError {
    repeatPassword.error.value = 'err_incorrect_input'.l10n;
  } on UpdateUserPasswordException catch (e) {
    switch (e.code) {
      case UpdateUserPasswordErrorCode.wrongOldPassword:
        repeatPassword.error.value = 'err_wrong_password'.l10n;
      case UpdateUserPasswordErrorCode.wrongCode:
        recoveryCode.error.value = 'err_wrong_code'.l10n;
      case UpdateUserPasswordErrorCode.confirmationRequired:
        repeatPassword.error.value = 'err_data_transfer'.l10n;
      case UpdateUserPasswordErrorCode.artemisUnknown:
        repeatPassword.error.value = 'err_unknown'.l10n;
    }
  } catch (e) {
    repeatPassword.resubmitOnError.value = true;
    repeatPassword.error.value = 'err_data_transfer'.l10n;
    rethrow;
  } finally {
    newPassword.status.value = RxStatus.empty();
    repeatPassword.status.value = RxStatus.empty();
    newPassword.editable.value = true;
    repeatPassword.editable.value = true;
  }
}