Home > Java > javaTutorial > How to Redirect Students, Teachers, and Admins to Their Respective Activities After Firebase Authentication?

How to Redirect Students, Teachers, and Admins to Their Respective Activities After Firebase Authentication?

Linda Hamilton
Release: 2024-12-10 18:02:10
Original
754 people have browsed it

How to Redirect Students, Teachers, and Admins to Their Respective Activities After Firebase Authentication?

Implementing Multi-User Redirection in a Firebase Voting App

In the context of a Firebase-based voting application, you are wondering how to redirect three distinct types of users (STUDENTS, TEACHERS, and ADMINS) to their respective activities after login. Here's a modified version of your code to cater to this requirement:

mAuthListener = new FirebaseAuth.AuthStateListener() {
  @Override
  public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
    FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();

    if (firebaseUser != null) {
      String uid = firebaseUser.getUid();
      DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
      DatabaseReference usersRef = rootRef.child("users").child(uid);

      ValueEventListener userListener = new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
          // Assert that the DataSnapshot is valid
          if (!dataSnapshot.exists()) {
            Log.e(TAG, "Error: DataSnapshot not found");
            return;
          }

          // Check the user type and redirect accordingly
          if (dataSnapshot.child("type").getValue(Long.class) == 1) {
            startActivity(new Intent(MainActivity.this, student.class));
          } else if (dataSnapshot.child("type").getValue(Long.class) == 2) {
            startActivity(new Intent(MainActivity.this, teacher.class));
          } else if (dataSnapshot.child("type").getValue(Long.class) == 3) {
            startActivity(new Intent(MainActivity.this, admin.class));
          }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
          Log.e(TAG, databaseError.getMessage());
        }
      };

      usersRef.addListenerForSingleValueEvent(userListener);
    }
  }
};
Copy after login

Key Changes:

  • The uidRef has been updated to point to the users node under the Firebase root.
  • Instead of checking only for the existence of a student, the code now checks the type child node to determine the user's type.
  • Three separate conditionals have been added to handle all three user types.

The above is the detailed content of How to Redirect Students, Teachers, and Admins to Their Respective Activities After Firebase Authentication?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template