Rule for Firestore that enforce user to write a document with id equals to its own uid

心不动则不痛 提交于 2019-12-06 06:23:32

resource.id can only be used to refer to an existing document in Firestore. It doesn't work if the document being written doesn't exist yet.

Instead, you can use the salesman wildcard in your match expression to determine if the user can write a particular document, even if it doesn't exist yet:

match /salesmen/{salesman} {
  allow read: if true;
  allow write: if request.auth != null && salesman == request.auth.uid;
}

It looks like you can also use request.resource.id to reference the id of the document that possibly has not been written yet:

match /salesmen/{salesman} {
  allow read: if true;
  allow write: if request.auth != null && request.resource.id == request.auth.uid;
}

Though I can't find that explicitly discussed in the reference documentation as of this moment.

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