logIn method

void logIn()

Implementation

void logIn() async {
  if (kDebugMode &&
      signInEmailTxtController.text.isEmpty &&
      signInPassTxtController.text.isEmpty) {
    signInEmailTxtController.text = "crubuddeilloyo-2395@yopmail.com";
    signInPassTxtController.text = "Qwerty?123";
  }

  if (!RegExp(r'^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$')
      .hasMatch(signInEmailTxtController.text)) {
    CustomFlashWidget.showFlashMessage(
      type: FlashType.error,
      title: "Invalid email",
      message: "Please enter a valid email",
    );
  } else if (signInPassTxtController.text.length < 8) {
    CustomFlashWidget.showFlashMessage(
      type: FlashType.error,
      title: "Invalid password",
      message: "Please enter a valid password",
    );
  } else {
    loadingDialog();
    AuthCredential cred = EmailAuthProvider.credential(
      email: signInEmailTxtController.text.trim(),
      password: signInPassTxtController.text.trim(),
    );

    try {
      mobileNumTxtController.clear();
      UserCredential userCredential =
          await FirebaseAuth.instance.signInWithCredential(cred);
      final loginResult =
          await Requests.createUser(userCredential.user?.uid ?? "");
      if (loginResult?['statusCode'] == 200) {
        if (['SUBSCRIBER', 'FOUNDER'].contains(loginResult?['role'])) {
          closeDialog();
          bool didUserCancelDialog = false;
          if(loginResult["phone"] == null || loginResult["phone"].toString().isEmpty){
            await ResidentCountryView.show(
              barrierDismissible: false,
              onConfirm: () async {
                closeDialog();
              },
              onPressClose: (){
                didUserCancelDialog = true;
                closeDialog();
              },
            );

            if(didUserCancelDialog){
              return ;
            }else {
              phoneNumberController.text =
                  mobileNumTxtController.text.replaceAll(
                      countryCodeController.text, "");
              bool result = await verifyPhone();
              if (!result) {
                return;
              }
            }
          }
          onLoginSuccessful(referralCode: loginResult?['referrerCode']);
        } else {
          _handleUnauthorizedAccess();
        }
      } else if (loginResult?['statusCode'] == 400 &&
          loginResult?['message'].contains('Invalid referralCode')) {
      ReferralCodePopup.show(
        onSubmit: (String refCode) async {
          final reSigninResult = await Requests.createUser(
              userCredential.user?.uid ?? "",
              referralCode: refCode
              );
          closeDialog();
          if (reSigninResult?['statusCode'] == 200 &&
              (reSigninResult?['role'] == 'SUBSCRIBER' ||
                  reSigninResult?['role'] == 'FOUNDER')) {
            closeDialog();
            await onLoginSuccessful(
                referralCode: reSigninResult?['referrerCode']);
          } else {
            clearLoginFields();
            _handleSignupError();
            closeDialog();
          }
        },
      );
      } else {
        _handleSignupError();
      }
    } on FirebaseAuthException catch (e) {
      print(e.toString());
      closeDialog();

      String errorMessage;
      String errorTitle = "Error";

      switch (e.code) {
        case "user-not-found":
          errorMessage = "This email does not exist. Please sign up first.";
          break;
        case "wrong-password":
        case "invalid-credential":
        case "invalid-login-credentials":
          errorTitle = "Invalid Credentials";
          errorMessage =
              "Your Email and Password combination is not correct. Please re-check and try again.";
          break;
        case "too-many-requests":
          errorTitle = "Too Many Attempts";
          errorMessage =
              "Too many sign-in attempts detected. To protect your account, we have temporarily blocked requests from this device. Please try again later.";
          break;
        default:
          errorMessage =
              e.message ?? "Something went wrong. Please try again.";
      }
      CustomFlashWidget.showFlashMessage(
        type: FlashType.error,
        title: errorTitle,
        message: errorMessage,
      );
      return;
    }
  }
}