'Is there a way to classify users in firebase? [duplicate]
I'm using firebase in my android project, and I have to types of user, advertiser and tester user. I have searched to how I can classify users in firebase but I didn't get anything helpful. so is there a way to do this?
Solution 1:[1]
Option 1: Maintain a user collection in Firestore
You could have a collection in Firestore that saves a document for each user. Each document could contain user-specific information (like type/role). Depending on your use case you can also lock it down with Security Rules. It's an easy way to achieve it, but it always involves another query to Firestore to check the current role if you need to.
Option 2: Custom Tokens
On server-side you can create Custom Tokens for Firebase Auth. There you could assign each user a type/role in Auth. This gives you the option to use them in Security Rules in Firestore and Real-time DB:
{
"rules": {
"premiumContent": {
".read": "auth.token.premiumAccount === true"
}
}
}
Note that you have to set custom tokens on the server; you can't do this on the client.
As @puf pointed out in the comments: you can set Custom Claims on existing providers.
This is also done on the server. Example: some user action triggers a Cloud Function, then in this Cloud Function you can access the user's Firebase Auth object and set the "claim" (which could be your type/role). This information is then also available in Security Rules (as shown above) and you can access all this also on the client.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
