1

How do I configure permissions so that a user can only read and write documents where they are the owner? my documents have a top level attribute called owner.

I read these docs
https://firebase.google.com/docs/firestore/security/secure-data?authuser=0#the_resource_variable

and it seems like this should work?

service cloud.firestore {
  match /databases/{database}/documents {
    match /analysis/{analysis} {
      allow read, write: if request.auth.email == resource.data.owner
    }
  }
}

however this doesn't seem to work. i continuously get insufficient permission error. What do i need to change?


Update:

After reading the docs @Doug Stevenson linked I decided to go with

service cloud.firestore {
  match /databases/{database}/documents {
    match /analysis/{analysis} {
      allow read, write: if request.auth.uid == resource.data.owner_uid;
    }
  }
}

So my original intent was that when we do a list operation:

a. only those documents belonging to a user are returned
b. only documents a user owns can be read or written by that user.

With the above configuration b. is accomplished.
how do I do accomplish a. ?

w--
  • 6,427
  • 12
  • 54
  • 92

2 Answers2

5

According to the documentation, you should use request.auth.token.email (not request.auth.email).

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
4

To answer your update question part (a):

I has a similar issue - I had my rules set correctly to allow that user to read and write. However, Firestore needs us to have our queries match the firestore database rules. So the query on the front should match what that user has access to. So my issue was I was asking for all products in the collection. What I needed to change it to was asking for all products in the collection where the "owner" of the product matched the logged in user, like this:

db.collection('products').where('owner', '==', firebase.auth().currentUser.uid).get()

Hope this helps

Ben Cochrane
  • 3,317
  • 1
  • 14
  • 16
  • thanks. i re asked my question seperately here https://stackoverflow.com/questions/47472230/firestore-permissions/47685677#47685677 and got the same answer – w-- Dec 08 '17 at 01:31