In firestore I want a user to only access a document if the user is in the teamid
mentioned in the document. Now I have a different collection called teams
where I have users mapped as { user_id = true }
. So I have the following in the Firestore rules
return get(/databases/$(database)/documents/teams/$(resource.data.teamid)).data.approvedMembers[request.auth.uid] == true;
Now this rule does not work and fails any request made to the database by the frontend. But when I replace $(resource.data.teamid)
with my actual teamid
value as follows,
return get(/databases/$(database)/documents/teams/234234jk2jk34j23).data.approvedMembers[request.auth.uid] == true;
... it works as expected.
Now my question is am I using resource
in a wrong way or is resource
not supported in get()
or exists()
queries in Firestore rules?
Edit Complete rules as follows
service cloud.firestore {
match /databases/{database}/documents {
function isTeamMember() {
return get(/databases/$(database)/documents/teams/$(resource.data.teamid)).data.approvedMembers[request.auth.uid] == true;
// return exists(/databases/$(database)/documents/teams/$(resource.data.teamid));
}
match /{document=**} {
allow read, write: if isTeamMember();
}
}
}
If you notice the commented out rule, exists()
does not work in this case either.
match
andallow get
statements where this applies? – Sam Stern