passwordOrCode property

TextFieldState passwordOrCode
latefinal

TextFieldState of the UserPassword input.

Implementation

late final TextFieldState passwordOrCode = TextFieldState(
  onChanged: (s) => s.error.value = null,
  onSubmitted: (s) async {
    final password = UserPassword.tryParse(s.text);

    if (password == null) {
      s.error.value = 'err_wrong_password'.l10n;
    } else {
      s.editable.value = false;
      s.status.value = RxStatus.loading();
      try {
        final code = ConfirmationCode.tryParse(s.text);

        // If text in the field is not even parsed as [ConfirmationCode], then
        // it is certainly a [UserPassword].
        if (code == null) {
          await _myUserService.removeUserEmail(email, password: password);
        } else {
          // Otherwise first try the parsed [ConfirmationCode].
          try {
            await _myUserService.removeUserEmail(email, confirmation: code);
          } on RemoveUserEmailException catch (e) {
            switch (e.code) {
              // If wrong, then perhaps it may be a password instead?
              case RemoveUserEmailErrorCode.wrongCode:
                await _myUserService.removeUserEmail(
                  email,
                  password: password,
                );
                break;

              default:
                rethrow;
            }
          }
        }
        s.clear();
        page.value = DeleteEmailPage.success;
      } on RemoveUserEmailException catch (e) {
        s.error.value = e.toMessage();
      } catch (e) {
        s.resubmitOnError.value = true;
        s.error.value = 'err_data_transfer'.l10n;
        s.unsubmit();
        rethrow;
      } finally {
        s.editable.value = true;
        s.status.value = RxStatus.empty();
      }
    }
  },
);