如何在Firebase中隔离用户数据?

时间:2019-06-12 23:22:42

标签: swift firebase google-cloud-firestore firebase-security-rules

我正在创建一个使用Firebase Cloud Firestore数据库的应用程序。结构似乎是集合/文档/字段。我正在考虑将用户ID用作集合名称的前缀,或者仅将userId用作字段。

我当前正在使用:

Firestore.firestore().collection("Events").  

可以将其更改为具有userId的前缀事件

我目前正在阅读使用中的所有内容:

reference(to: "Events").addSnapshotListener{ (snapshot, _) in
    guard let snapshot = snapshot else {return}
    for document in snapshot.documents {
        // code here
    }
}

1 个答案:

答案 0 :(得分:1)

在集合名称上使用前缀不是一个好主意。这不适用于安全规则。

每个用户数据的通常结构是具有一个ID为用户ID的文档的集合。然后,您可以在该文档ID下的子集合中进一步组织其他数据。

/users
  /uid1
  /uid2
    /events
    /likes
    /history

然后,您可以非常轻松地write security rules using the user's UID