confirmSignUpCode method

Future<void> confirmSignUpCode(
  1. String username,
  2. String code
)

Confirm the registration of a particular username with the given code

Returns bool indicating if the registration was successful

Implementation

Future<void> confirmSignUpCode(
  String username,
  String code,
) async {
  emit(state.startLoading(confirmSignUpLoading));

  try {
    final isConfirmed = await provider.confirmSignUpCode(username, code);
    if (isConfirmed) {
      if (state.awaitingVerification?.type == VerificationType.email) {
        emit(state.copyWith(
          user: state.user.copyWith(
            emailAddressVerified: true,
          ),
          resetAwaitingVerification: true,
        ));
      } else if (state.awaitingVerification?.type == VerificationType.sms) {
        emit(state.copyWith(
          user: state.user.copyWith(
            mobilePhoneVerified: true,
          ),
          resetAwaitingVerification: true,
        ));
      } else {
        emit(state.copyWith(
          resetAwaitingLogin: true,
          resetAwaitingVerification: true,
        ));
      }
    } else {
      emit(
        state
            .addMessage(
              Message.alert(_localizations.unableToConfirmSignUp),
            )
            .copyWith(
              resetAwaitingVerification: true,
            ),
      );
    }
  } on InvalidCodeException catch (error, stackTrace) {
    emit(state.addMessage(
      Message.error(_localizations.invalidVerificationCode),
      error,
      stackTrace,
    ));
  } on ExpiredCodeException catch (error, stackTrace) {
    emit(state.addMessage(
      Message.error(_localizations.expiredVerificationCode),
      error,
      stackTrace,
    ));
  } on ConfirmSignUpCodeException catch (error, stackTrace) {
    emit(state.addMessage(
      Message.error(
        _localizations.confirmSignUpError(error.message),
      ),
      error,
      stackTrace,
    ));
  } finally {
    emit(state.endLoading(confirmSignUpLoading));
  }
}