I need a firestore rule that allows access for user ids that are in the users field in a document:
Document 1:
uid: 13423425,
...,
users: [{uid: 123, email: [email protected]}, {uid: 456, email: [email protected]}]
User 123 should be able to access this field, but a user 999 would not be able to access this document. How do I write a firestore rule that allows me to check whether a given user id is in the users field? I tried using the IN operator but it doesn't work in this case since it is an array of maps (not an array of strings or numbers). Essentially I'm trying to write a rule like this (using a map):
function isAllowed(sometableId) {
return request.auth.uid != null
&& request.auth.uid in get(/databases/$(database)/documents/sometable/$(sometableId)).data.users.map(user => user.uid);
}
match /sometable/{sometableId} {
allow create: if request.auth.uid != null;
allow read, write: if isAllowed(sometableId);
}