Search code examples
javascriptfirebasegoogle-cloud-firestorefirebase-security

How to set Firestore Rule searching for a reference


I'm trying to request a user's customerId based on the user's uid, so that I can use that customerId for future reads/write within the application based on the denormalized structure of other collections.

schema

match /customers/{customerId} {
     allow read: if debug(/databases/$(database)/documents/users/$(request.auth.uid) in resource.data.users);
     // allow list: if request.auth.uid in resource.data.users;
     allow write: if false;
}
const userRef = firestore
                .collection("users")
                .doc("AKwPD9IUq8oyLYb454XaA7JUmpyO")
await firestore
  .collection("customers")
  .where("users", "array-contains", userRef).get()

FirebaseError: Null value error. for 'list' @ L13, Null value error. for 'list' @ L14

When I change to read: true or list: true the query is allowed (so I know it's the right match, just wrong rules)


Solution

  • So angry - the answer is https://stackoverflow.com/a/51115385/1166285

    Firebase emulators were on different versions than my project's firebase packages, so npm install -g firebase-tools fixed it.

    Wasted like 6 hours 😡🤬 i hope someone working at Firebase sees this!!