【问题标题】:Firestore Rules: Allow Users only to write to their own document and fieldsFirestore 规则:仅允许用户写入自己的文档和字段
【发布时间】:2020-02-07 16:08:05
【问题描述】:

如何只允许用户在 Firestore 中写入自己的文档和字段?


注意!
我已将 uid 更改为电子邮件。


这是我尝试过但不起作用的规则:
 service cloud.firestore {
  match /databases/{database}/documents {

    match /users/request.auth.token.email {
         allow write: if get(path("databases/" + database + "/documents/users/" + request.auth.token.email)).data.uid == request.auth.uid;
    }
  }
}

【问题讨论】:

  • 我强烈建议不要使用电子邮件而不是 UID。电子邮件地址可以更改,但用户的 UID 永远不会更改。 UID 是文档 ID 的更好选择。

标签: javascript firebase google-cloud-firestore firebase-security


【解决方案1】:

成功了! =)

service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{email} {
       allow update, delete: if request.auth.token['email'] == email;
       allow create: if request.auth.uid != null;
    }
  }
}

注意!此规则将FIREBASE 模拟器 的测试中起作用,但它会在我的网站上真正起作用。 p>

【讨论】:

    猜你喜欢
    • 2020-10-23
    • 1970-01-01
    • 2020-09-22
    • 1970-01-01
    • 2019-11-02
    • 1970-01-01
    • 1970-01-01
    • 2018-10-01
    • 2019-09-10
    相关资源
    最近更新 更多