sendVerificationCode method

Future<void> sendVerificationCode()

Implementation

Future<void> sendVerificationCode() async {
  try {
    state = state.copyWith(isLoading: true, showError: false, errorMessage: null);

    if (state.verificationType == VerificationType.phone) {
      final request = PhoneVerificationStartRequest(
        phoneNumber: state.identifier,
        countryCode: state.countryCode?.code,
      );

      await ApiClient.shared.perform(request);

      state = state.copyWith(
        isLoading: false,
        isVerificationSent: true,
      );

    } else {
      final request = EmailVerificationStartRequest(
        emailAddress: state.identifier,
      );
      await ApiClient.shared.perform(request);

      state = state.copyWith(
        isLoading: false,
        isVerificationSent: true,
      );
    }
  } catch (e) {
    String errorMessage = e.toString();
    if (_context != null) {
      errorMessage = VerificationStrings(_context!).failedToSendCode(e.toString());
    }

    state = state.copyWith(
      isLoading: false,
      showError: true,
      errorMessage: errorMessage,
    );
  }
}