How to log in two different types of users to different activities automatically without having to log in again?
This is a hack I used to solve the same issue. (Please note that this method may have security issues and I prefer using Firebase Custom Claims using Firebase Admin).
Create a LoginActivity
using which you log in the user and after logging in, access the user type(admin, student, staff, paid whatever) in an UserTypeSelectorActivity
and form this activity you can pass the user type to other activities using Intent data or shared preferences (which you feel better according to your app)
public class UserTypeSelectorActivity extends AppCompatActivity {
// Firebase Variables
FirebaseUser firebaseUser;
FirebaseDatabase firebaseDatabase;
DatabaseReference firebaseDatabaseReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("Activity Name", getLocalClassName());
// Initializing Firebase Variables
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
firebaseDatabase = FirebaseDatabase.getInstance();
firebaseDatabaseReference = firebaseDatabase.getReference();
if (firebaseUser != null) {
firebaseDatabaseReference.child("my_app_user").child(firebaseUser.getUid())
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Check user type and redirect accordingly
if (dataSnapshot.child("admin").exists()) {
Boolean admin = dataSnapshot.child("admin")
.getValue().toString().equals("true");
if (admin) {
startActivity(new Intent(UserTypeSelectorActivity.this,
AdminActivity.class));
finish();
} else {
startActivity(new Intent(UserTypeSelectorActivity.this,
ClientActivity.class));
finish();
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
}
Note: A precautionary warning that this is not a proper solution. It is how I have implemented in my app.
Can Firebase handle multiple types of user accounts?
Firebase Auth accounts don't discriminate between user types, nor does it offer any profile settings for that. User type is a concept that you have to implement for yourself using your own definitions of "admin" or "standard" or whatever it is you need.
Related Topics
Android: Simulate Wifi in the Emulator
How to Handle Multiple Application Classes in Android
Cannot Start Activity Background in Android 10 [ Android Q ]
Detect When Android V2 Maps Has Loaded
How to Create Parallelogram Shape Background
Installation Error: Install_Failed_Older_Sdk
How to Completely Kill/Remove/Delete/Stop an Asynctask
How to Put an Image in an Alertdialog? Android
How to Pass a File Path Which Is in Assets Folder to File(String Path)
Why Shouldn't I Use System.Out.Println() in Android
Get Temperature of Battery on Android
Android Device as a Receiver for A2Dp Profile
Android: Backspace in Webview/Baseinputconnection
Can't Apply System Screen Brightness Programmatically in Android
How to Pop Fragment Off Backstack
Limit Scrolling and Zooming Google Maps Android API V2