How to Authenticate Two Types of Users (Student and Driver) in My Android App Using Firebase

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



Leave a reply



Submit