Firestore permissions

六眼飞鱼酱① 提交于 2020-01-07 04:57:28

问题


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. ?


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/47472230/firestore-permissions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!