Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to separate user roles in firebase?

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();
}
like image 240
Gian7 Avatar asked Jul 29 '16 19:07

Gian7


People also ask

What are the different roles that can be assigned to a user in Firebase?

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.

How do you prevent simultaneous logins of the same user with Firebase?

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.

How do you create an Admin module for managing Firebase users access and roles?

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 .

Can you change user UID Firebase?

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.


1 Answers

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) {}
});
like image 71
Wilik Avatar answered Sep 20 '22 12:09

Wilik