0

Im new to flutter. I am developing a complaint management app. While logging in there are 2 buttons, sign in (google) and admin sign in. the sign in button is for users and will redirect to home, the admin button will verify with a password and sign in with google. (redirect to admin page) The problem is, once the app is reopened, I am using streambuilder to look for snapshot.data and go to home page. But even for admin its going to home page and not admin after restarting app. I tried to set up a boolean (isLoggedInUserAdmin) and set it 'true' when admin signs in and 'false' when (adminSignsOut). In streambuilder, I am doing

StreamBuilder(
        stream: AuthMethods().authChanges,
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return const Center(
              child: CircularProgressIndicator(),
            );
          }

          if (snapshot.hasData) {
            

            if (AdminLoginScreen.isLoggedInUserAdmin && !AdminComplaintsScreen.hasAdminSignedOut) { //checks if the logged in user is admin and they havent signed out
              
              return AdminHomeScreen();
            } else {
              return HomeScreen(); //its normal user, so return to home
            }
          }

          return const LoginScreen(); //if no snapshot.data, return to LoginScreen
        },
      ),

Be it Admin or User, once app is restarted, it goes to Normal Home Screen. How do I identify the admin and return him to AdminHomePage ??

Im storing all the users' name in users collection and admins in admins collection in firebasefirestore.

Please help me, I need this project to be completed in a week. PLEASE!

1 Answer 1

0

You can use a function to create a user document in Firestore database every time a new user is added to the Authentication database.

In the newly created user document, you can assign a role to that user.

Then all you have to do is query the 'role' field in the user document that was generated using a future Builder and route the user to the desired screen based on the role returned from Firebase.

The best part is that you can query this role in your database rules, stopping anyone without permission from changing their role.

1
  • Please help me with the code as I'm new to flutter.
    – Shakthib
    Commented Jul 20, 2022 at 20:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.