How do I add user role authentication in firebase android app? From what I see, firebase only has email and password authentication. I want to develop an android app that has 2+ user roles. For example, if a normal member logs in, he will go to normal_memberActivity and if he's a premium member, he'll go to premium_memberActivity. How do I add roles in firebase? Wasn't able to find a similar problem like mine.
Normally, I would do it like this:
String role = intent.getStringExtra("role");
if (role.equals("admin")){
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, new AdminHomeFragment()).commit();
}else{
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, new NormalFragment()).commit();
}
In the Firebase console, you can assign any of the basic roles (Owner, Editor, Viewer), the Firebase Admin/Viewer roles, or any of the Firebase predefined product-category roles.
Set the rules of your database We want the user not to be able to add a connected device if the value is above 2. This is ensured by the following rules. The user will only be able to write on the database if the value he sent is under or equals 2, meaning they can only have 2 simultaneous connections.
We start by creating the userCreationRequest Firestore document in the corresponding Collection, with a the status field set to Pending ⁶. We then create the new user by passing to the createUser() method of the Admin SDK an object of type admin. auth. CreateRequest .
An ID that uniquely identifies a user. By default, Firebase uses randomly generated 28-character strings. The UID of a user cannot be changed, but when creating a new user through Firefoo, you can choose a custom UID.
You can add a roles node in the firebase database storing the user's uid and their role.
"roles" : {
"uid1" : "normal",
"uid2" : "premium",
"uid3" : "normal",
}
Then you can get the role value after the user is successfully signed in
ref.child("roles").child(firebaseUser.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String role = dataSnapshot.getValue(String.class);
// check role and replace fragment
}
@Override
public void onCancelled(DatabaseError databaseError) {}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With