How to Create Different User Groups in Firebase

How to create different user groups in Firebase?

This is an incredibly broad topic and lots of it depends on how you implement the various parts of your app. Below is one of the many possible answers, just in an effort to get you started with some links.

If your app stores its data in one of Firebase database offerings (Realtime Database, or Cloud Firestore), or if it stores files in Cloud Storage through Firebase, then you'll likely want to store the role of each user as a custom claim in the profile of that user.

The Firebase Authentication documentation shows how to set such custom claims, for example how to set the admin property of a user to true from a Node.js script:

admin.auth().getUserByEmail('user@admin.example.com').then((user) => {
// Confirm user is verified.
if (user.emailVerified) {
// Add custom claims for additional privileges.
// This will be picked up by the user on token refresh or next sign in on new device.
return admin.auth().setCustomUserClaims(user.uid, {
admin: true
});
}
}).catch((error) => {
console.log(error);
});

Similarly you could set your user roles in a role property. Then you can check in the server-side security rules of the Realtime Database, Cloud Firestore, or Cloud Storage if the user has a role that allows them access to the specific data they're trying to access.

In the client-side code you can then decode the user's token to get access to the same claims and optimize the UI for them

How to use Firebase Auth for different user groups with different requirements

A single Firebase project has a single store of users.

Since all it does is store the credentials for the user, there isn't a need for multiple lists in Firebase Authentication itself.

If your application needs to distinguish different types of users, you can:

  1. Store those lists of users elsewhere, such as in one of the Firebase databases. For example, see here.
  2. Store a custom claim in each user's profile to indicate the list(s) they belong to. For documentation, see here.
  3. Set up different projects, one for each type of user.

I've put these in order of most common to least common, so you might want to study and consider them in that order.

How to create 2 different User group in Firebase AUTH with Flutter

There is no such concept directly implemented in Firebase Auth as far I know, but you have essentially 2 options:

  • as a custom claim in the Firebase Authentication token for that user

  • in the database using a User collection with documents associated with your users.

You should be setting the role from within a trusted environment (i.e. Cloud Functions or even manually) as otherwise anyone can change their own role if you do not secure your documents by access rules.

Once set in either of these locations, you can access the role information in your client-side code.

you may want to have a look at: https://www.youtube.com/watch?v=oFlHzF5U-HA

how to make singups and signins with different group of users

Storing this information in the user's display name can work. You can read it back from there next time, and take action in your application's client-side code. But note that this means that any user can change their role, since they can also call the same code to update their profile. If that is not a concern for your app, then this approach sounds like it would work.

If malicious users should not be able to change their role, then you shouldn't set that role from the client-side application code. In that case, you can set the role from a server (or your development machine, or Cloud Functions) using the Admin SDK. Since the Admin SDK runs in a trusted environment, it has expanded privileges and can update the profile of any user. So the Admin SDK could update the display name of the user in the same way you have in mind.

But this still isn't secure, since you're still setting a property that anyone can modify for their own profile. Again... if that is no problem for your app that is fine, but if the use-case requires that you can rely on the property to be correct, we have to keep looking elsewhere.

The Admin SDK can set additional so-called claims on a user profile that client-side code can't modify. Such claims are for things that affect the permissions of the user, such if the user is an admin, or what role/group your users belong to. This sounds quite close to what you are describing, so can also be used. And this time, only your code that runs in a trusted environment will be able to do so.

Finally, you could store the additional information about a user in the database. It's quite common to have a collection (Users or Profiles) in the database, where you store a document for each user (with the document name being User.uid). You create the document when the user first signs in, and update whenever you need to. You can do this from the client-side code (if there is no need to control what gets written), or from code that runs in a trusted environment (such as your development machine, a server you control, or Cloud Functions) if you do need to keep control. A big advantage of this approach is that all users can potentially see the information in this collection, where the client-side Authentication SDK only allows a user to read their own user profile.

For more on this, see:

  • Adding new data to firebase users (in which I essentially list the same options with fewer words)
  • Add extra User Information with firebase (store the information in the realtime database)
  • Associate Firebase Users to Database Records (also using the realtime database for the additional information)
  • Cloud Firestore saving additional user data
  • this video explaining custom claims
  • and many more previous questions on this topic

Adding user to a single app within a firebase project

All user information in a project is shared between all apps in that project. There is no way to partition the user information for a specific app.

There are two common cases for this request:

  1. The apps have nothing to do with each other. In this case you should put each app in its own project.
  2. The apps grant access for different types of users. In this case you will need to build your own role-based access system on top of Firebase, and allow only user with the correct role to use each app.

Also see:

  • How to create different user groups in Firebase?
  • How to create two types of users(Client , Freelancer. for example) while Auth using firebase in a flutter app?
  • more results from this search

how to create different users using reactjs and firebase

You have set the collection to doctors in the following code snippet:

const ref = db.collection("doctor")

If you want to add a different user to a different firebase collection you just need to replace the collection name:

const ref = db.collection("patient")



Related Topics



Leave a reply



Submit