Firestore write security rule for specific user using auth names

空扰寡人 提交于 2021-01-29 21:11:17

问题


New to Firestore security rules and is trying to understand it. I'm trying to write a security rule that allow only admins in my collection to write data and every one to read it.

The collection of admins has document ids as admin names, that is for example, "Mary Lane". Within the documents I've fields:

email: "userMailId@mail.com"
uId: "firestore_user_Id"

The uId is the id of Firestore user id. The data to write is an object Message and is:

new Message(uId, title, messageBody, timestamp)

Currently I'm trying to match the request.auth.uid with the id of the admins that are stored within the collection:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read;
      allow write: if request.auth.uid == resource.data.uId;
    }
  }
}

Is this is write or am I doing something wrong. Any help is appreciated.


回答1:


Since the admin's name is not available in this rule allow write: if request.auth.uid == resource.data.uId;, there is no way to look up the document to check it they're an admin.

You'll need a collection where you keep a document for each admin with their UID as the document key/name. Once you have that, you can check for the existence of such a document in the rule with:

allow write: if exists(/databases/$(database)/documents/admins/$(request.auth.uid));

Also see the documentation on accessing other documents.



来源:https://stackoverflow.com/questions/52241725/firestore-write-security-rule-for-specific-user-using-auth-names

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