linkPhoneWithOAuth method

Future<UserCredential?> linkPhoneWithOAuth(
  1. User currentUser
)

Implementation

Future<UserCredential?> linkPhoneWithOAuth(User currentUser) async {
  try {
    resultPin.value = '';
    if (mobileNumTxtController.text == null ||
        mobileNumTxtController.text.isEmpty) {
      await ResidentCountryView.show(
        barrierDismissible: false,
        onConfirm: () async {
          closeDialog();
        },
      );
    }

    if (mobileNumTxtController.text.isEmpty) {
      CustomFlashWidget.showFlashMessage(
        type: FlashType.error,
        title: "Error",
        message: "Please enter a valid phone number!",
      );
      return null;
    } else if (!isValidPhoneNumber(mobileNumTxtController.text)) {
      CustomFlashWidget.showFlashMessage(
        type: FlashType.error,
        title: "Error",
        message: "Please enter a valid phone number containing only digits",
      );
      return null;
    }
    if(currentUser.email == null) return null;
    bool phoneExist = await checkPhoneExistence(
      phoneNumberController.text.trim(),
    );

    if (phoneExist) return null;

    ConfirmationResult confirmationResult =
        await currentUser.linkWithPhoneNumber(mobileNumTxtController.text);
    _timer?.cancel();
    startTimer();
    await OtpVerificationView.show(
      barrierDismissible: false,
      onConfirm: () {

        closeDialog();
      },
      onResend: () async {
        try {
          resultPin.value = '';
          confirmationResult = await currentUser
              .linkWithPhoneNumber(mobileNumTxtController.text);
          CustomFlashWidget.showFlashMessage(
            type: FlashType.success,
            title: "Success",
            message: "Otp code resent successfully!",
          );
          cancelTimer();
          startTimer();
        } catch (e) {
          CustomFlashWidget.showFlashMessage(
            type: FlashType.success,
            title: "Error",
            message: e.toString(),
          );
        }
      },
    );
    if (resultPin.value != null && resultPin.value.isNotEmpty) {
      UserCredential userCredential =
          await confirmationResult.confirm(resultPin.value);
      onLoginSuccessful();
      return userCredential;
    } else {
      CustomFlashWidget.showFlashMessage(
          title: 'Error', message: "Otp code missing.");
      return null;
    }
  } catch (e) {
    print(e.toString());
    if (e.toString().contains('invalid-verification-code')) {
      CustomFlashWidget.showFlashMessage(
          title: 'Error', message: "Please enter correct otp.");
      cancelTimer();
      resultPin.value = '';
      return await linkPhoneWithOAuth(currentUser);
    } else {
      showRefinedErrorMessage(e);
      return null;
    }
  }
}