How do I get Flutter to check if a value in a child node for Firebase Realtime Database matches user input (Android)?

Viewed 32

I'm completely new to flutter development and I am making a login for admins in my flutter mobile app.

The email address used in the login already exists in Firebase Authentication, but before I have it proceed to logging in the app, I first need to check if the inputted email address is stored in Realtime Database. How do I go about this in terms of code?

The flow is like this: user inputted "abc@gmail.com" as email address for login. App checks rtdb if there is a value of "abc@gmail.com", if true, proceed with login.

I understand I have to use

var rtdbref =
      FirebaseDatabase.instance.reference().child("Admins").child("adminEmail");

but after that I have no idea how to have Flutter read the values. Any help is very much appreciated! Thank you :)

Image for rtdb reference: screenshot of rtdb

Code I have so far:

final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
  var rtdbref =
      FirebaseDatabase.instance.reference().child("Admins").child("adminEmail");
  void loginAdmin(BuildContext context) async {
    try {
      final User? firebaseUser =
          (await _firebaseAuth.signInWithEmailAndPassword(
                  email: emailTextEditingController.text,
                  password: passwordTextEditingController.text))
              .user;

      if (firebaseUser != null) {
        var isAdmin = await rtdbref.child("Admins").once();
        if (isAdmin.value["adminEmail"] == emailTextEditingController.text) {
          //check if admin here
          if (firebaseUser.emailVerified) {
            //if (firebaseUser.emailVerified) {
            usersRef.child(firebaseUser.uid).once().then((DataSnapshot snap) {
              if (snap.value != null) {
                Navigator.pushNamedAndRemoveUntil(
                    context, Navbar.idScreen, (route) => false);
                //displayToastMessage("Homepage", context);
              }
            });
          } else {
            displayToastMessage("Please verify your account", context);
            await firebaseUser.sendEmailVerification();
          }
        } else {
          _firebaseAuth.signOut();
          displayToastMessage("Account does not exist", context);
        }
      }
    } on FirebaseAuthException catch (e) {
      if (e.code == 'wrong-password') {
        displayToastMessage("Wrong password", context);
      }
      if (e.code == 'too-many-requests') {
        displayToastMessage("Please try again after 2 minutes", context);
      }
      if (e.code == 'user-not-found') {
        displayToastMessage("User not found", context);
      }
    }

    //displayToastMessage("Admin login test", context);
  }

Update: I've attempted the following but still cannot seem to get it to read the adminEmail values of node Admins. What am I missing here? I feel like im so close

DatabaseReference adminsRef =
    FirebaseDatabase.instance.reference().child("Admins");
var isAdmin = adminsRef
            .orderByChild("adminEmail")
            .equalTo(emailTextEditingController.text);
0 Answers
Related