Swift Firebase UID与文档id和Firestore规则

Swift Firebase UID与文档id和Firestore规则,swift,google-cloud-firestore,firebase-security,Swift,Google Cloud Firestore,Firebase Security,大家好,我对Firebase用户UID和Firestore文档ID userId有点困惑???。。。并寻求一些帮助:- 通过创建一个用户,我得到一个UID并将其写入数据库 let db = Firestore.firestore() db.collection("user").addDocument(data: [ "name": "confused", "uid": result!.uid ]) 通过这样做,我得到了一个标记为绿色的唯一文档id,我认为它也是用户id: 我想要实现的是

大家好,我对Firebase用户UID和Firestore文档ID userId有点困惑???。。。并寻求一些帮助:-

通过创建一个用户,我得到一个UID并将其写入数据库

 let db = Firestore.firestore()
 db.collection("user").addDocument(data: [
 "name": "confused",
 "uid": result!.uid ])
通过这样做,我得到了一个标记为绿色的唯一文档id,我认为它也是用户id:

我想要实现的是,用户只能读写绿色的文档,而不能读写红色的其他文档

因此,我使用了以下规则

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure the uid of the requesting user matches name of the user
    // document. The wildcard expression {userId} makes the userId variable
    // available in rules.
    match /user/{userId} {
      allow read, update, delete: if request.auth.uid == userId;
      allow create: if request.auth.uid != null;
    }
  }
}
那么UID和文档ID userId???他们应该有联系吗?但我真的不明白?!在我的应用程序中,我想检索用户的文档id,以便稍后在http触发器上使用它,但我只能获取UID

print(Auth.auth().currentUser!.uid)

有什么想法吗?或者我完全弄错了吗?

使用用户的UID作为他们自己文档的ID是正常的。现在,您正在使用addDocument,它告诉Firestore为文档分配一个随机ID。这样,安全规则将无法按预期工作,因为Firebase Auth分配的ID永远不会与Firestore分配的文档ID匹配。您应该改为使用setDocument并指定Firebase Auth中的UID作为要写入的文档ID。

好的,这很有意义,但是规则不正确-对吗?或者为什么可以编写数据库?对不起,我不明白你在问什么。我使用了Firestore规则,所以用户只能在屏幕截图中绿色地读/写文档。正如您所指出的,我犯了错误,并且不等于文档ID和UID。但是我仍然能够编写数据库——为什么可能呢?如果没有看到您的新代码和规则,就不可能说。请提出一个新问题,说明你的新情况,以及什么事情没有按你期望的方式进行。