Ad

Check If Auth.id Is In Resource Data Map With Firestore.rules?

I struggle to find a way to create a rule that allow read if a user is a participant in a document.

I have a collection of documents where "participants" in each document is defined as an array of objects. I would like to allow read, when the user's auth.uid is equal to the userId attribute of one of the participant objects.

My document looks like this in firestore:
enter image description here

Now in my firestore.rules I would like to write something like:

if resource.data.participants.find(p => p.userId == request.auth.uid)

...but that is not possible

Is there any way to put this into a function that will do the or another way to get around it?

br, Simon

Ad

Answer

This type of check is currently not possible with security rules, because there is no iteration in the language, and there are no map() or filter() type functions available on list type objects.

You always welcome to file a feature request for the latter (iteration will likely never be part of the language, but list map and filter are possibilities).

Ad
source: stackoverflow.com
Ad