login method

void login()

Implementation

void login() async {
  //TODO: remove this
  if (kDebugMode && userNameEmailTextController.text.isEmpty) {
    userNameEmailTextController.text = 'admin@myneogroup.com';
    passwordTextController.text = 'NEO__123';
  }
  if (!RegExp(r'^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$')
          .hasMatch(userNameEmailTextController.text) ||
      passwordTextController.text.length < 6) {
    DefaultSnackbar.show(
        "Email and password", "Please enter the valid email and password");
    return;
  }

  try {
    Get.dialog(
        barrierColor: ColorHelper.loaderBarrierColor,
        barrierDismissible: false,
        const LoadingDialog());
    AuthCredential cred = EmailAuthProvider.credential(
      email: userNameEmailTextController.text.trim(),
      password: passwordTextController.text.trim(),
    );

    try {
      UserCredential userCredential =
          await FirebaseAuth.instance.signInWithCredential(cred);
      final loginResult =
          await Requests.createUser(userCredential.user?.uid ?? "");
      if (loginResult?['statusCode'] == 200) {
        print("here 1" +loginResult?['role']);
        if (loginResult?['role'] == 'SUPER_ADMIN' ||
            loginResult?['role'] == 'AGENT') {

          closeDialog();
          onLoginSuccessful(false);
        } else {
          FirebaseAuth.instance.signOut();
          DefaultSnackbar.show("Access denied",
              "You are not authorized to access this page, You will be redirected soon.");
          await Future.delayed(
            const Duration(seconds: 3),
          );
          html.window.location.href = 'https://nc-staging.myneoone.com/';
        }
      } else {
        FirebaseAuth.instance.signOut();
        closeDialog();
        DefaultSnackbar.show("Error",
            "Unable to sign in. Please make sure you entered correct credentials");
      }
      closeDialog();
    } on FirebaseAuthException catch (e) {
      closeDialog();
      if (e.code == "user-not-found") {
        DefaultSnackbar.show(
            "Error", "This email does not exist. Please sign up first");
        return;
      } else if (e.code == "wrong-password" ||
          e.code == "invalid-credential") {
        {
          DefaultSnackbar.show("Invalid Credentials",
              "Your Email and Password combination is not correct. Please re-check and try again.");
        }
        return;
      } else {
        DefaultSnackbar.show("Error", e.message ?? "");
        return;
      }
    }
  } on FirebaseAuthException catch (e) {
    closeDialog();
    // Handle Firebase authentication errors
    if (e.code == "user-not-found") {
      DefaultSnackbar.show(
          "Error", "This email does not exist. Please sign up first");
    } else if (e.code == "wrong-password" || e.code == "invalid-credential") {
      DefaultSnackbar.show("Invalid Credentials",
          "Your Email and Password combination is not correct. Please re-check and try again.");
    } else {
      DefaultSnackbar.show("Error", e.message ?? "An error occurred");
    }
  }
}