daCoda
daCoda

Reputation: 3865

Read Firebase Firestore document if it document's property matches a request property

I'm sure this is a popular question but I can't find the question/answer!

Here's my Firestore rule:

service cloud.firestore {
  match /databases/{database}/documents {
  
    match /cars/{document} {
        allow read: if request.auth.uid == document.uid;
    }
   
  }
}

Here's a sample data: enter image description here

Upvotes: 0

Views: 281

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317958

If you want to refer to document data in rules, the syntax is like this:

allow read: if request.auth.uid == resource.data.uid;

This will work when the client performs a get() of the individual document, and they are signed in with a UID that matches the uid field of the document.

I suggest reviewing the documentation on authentication and data validation.

Upvotes: 2

Related Questions