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); } } };
Key Changes:
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!