Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use UID as map key in Firebase Security Rules

I'm building a chat app with Firestore and each chat room has a map of participants like so:

enter image description here

The keys are the corresponding UIDs of the users. I want to use this map to decide which user can access a chat. In my security rules, I currently check if the logged-in user's UID exists in this map to allow read and write access:

allow read, write: if request.auth.uid in resource.data.participants

But this only checks if the user is in this map, not if the value is actually true. I want to check that as well.

What I basically want is something like this:

allow read, write: if resource.data.participants.request.auth.uid == true

But this does not actually work. How do I use the current user's UID as the key in a map check?

like image 463
Florian Walther Avatar asked Oct 26 '25 12:10

Florian Walther


1 Answers

Use square brackets to index into the Map using any string expression you want:

allow read, write: if resource.data.participants[request.auth.uid] == true
like image 148
Doug Stevenson Avatar answered Oct 29 '25 04:10

Doug Stevenson