GunJack
GunJack

Reputation: 1968

How to set different rules for different collection of users for accessing same firestore collection?

I have a collection of data, say, products and two different set of users merchants and users. How do I grant read write access to merchants on products and only read access to users?

match /users/{userId} {
   allow read: if request.auth.uid == userId;
}

The above rule only defines access for a single users.

Upvotes: 0

Views: 40

Answers (1)

l1b3rty
l1b3rty

Reputation: 3642

Starting point:

match /users/{userId} {
   allow read: if request.auth.uid == userId;
}
match /products/{productid} {
   // Anyone authenticated can read
   allow read: if request.auth.uid != null;
   // Only merchants can write
   allow write: if get(/databases/$(database)/documents/users/$(request.auth.uid)).data.type == 'merchant'
}

Assuming users have field type with possible values ['user', 'merchant']

More info here

Upvotes: 1

Related Questions