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 (recoveryPassword.error.value != null ||
      recoveryRepeatPassword.error.value != null ||
      recoveryCode.error.value != null) {
    return;
  }

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

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

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

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

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

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

  recoveryPassword.editable.value = false;
  recoveryRepeatPassword.editable.value = false;
  recoveryPassword.status.value = RxStatus.loading();
  recoveryRepeatPassword.status.value = RxStatus.loading();

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

    page.value = IntroductionStage.signInWithPassword;
  } on FormatException {
    recoveryRepeatPassword.error.value = 'err_incorrect_input'.l10n;
  } on ArgumentError {
    recoveryRepeatPassword.error.value = 'err_incorrect_input'.l10n;
  } on UpdateUserPasswordException catch (e) {
    switch (e.code) {
      case UpdateUserPasswordErrorCode.wrongOldPassword:
        recoveryRepeatPassword.error.value = 'err_wrong_password'.l10n;
      case UpdateUserPasswordErrorCode.wrongCode:
        recoveryCode.error.value = 'err_wrong_code'.l10n;
      case UpdateUserPasswordErrorCode.confirmationRequired:
        recoveryRepeatPassword.error.value = 'err_data_transfer'.l10n;
      case UpdateUserPasswordErrorCode.artemisUnknown:
        recoveryRepeatPassword.error.value = 'err_unknown'.l10n;
    }
  } catch (e) {
    recoveryRepeatPassword.resubmitOnError.value = true;
    recoveryRepeatPassword.error.value = 'err_data_transfer'.l10n;
    rethrow;
  } finally {
    recoveryPassword.status.value = RxStatus.empty();
    recoveryRepeatPassword.status.value = RxStatus.empty();
    recoveryPassword.editable.value = true;
    recoveryRepeatPassword.editable.value = true;
  }
}