Firebase:如何構建公共/私有用戶數據當然,我的數據庫中的用戶擁有可以公開訪問的信息以及他們應該看到的其他信息。我正在考慮兩種不同的方式來實現它。選項1:有/users/$uid只由用戶讀取,并有/users/$uid/profile被任何人讀取。選項2:/users/$uid僅由該用戶保持可讀性且具有/profiles/$uid公共性。這遵循了更扁平的數據結構的建議,但我不知道在這種情況下它是如何更好的。
2 回答
海綿寶寶撒
TA貢獻1809條經驗 獲得超8個贊
了解“更平坦”結構為何更好的最簡單方法是查看如何保護它以及如何實現功能。
你的第一個結構是:
users: {
uidOfJacob: {
stackId: 884522,
ssn: "999-99-9999",
profile: {
displayName: "Jacob Philips"
}
},
uidOfPuf: {
stackId: 209103,
ssn: "999-99-9999",
profile: {
displayName: "Frank van Puffelen"
}
}}你可以用以下方法保護它:
{
"rules": {
"users": {
"$uid": {
".read": "auth.uid == $uid",
".write": "auth.uid == $uid"
"profile": {
".read": true
}
}
}
}}獲取公共信息的主要原因之一是能夠顯示該信息的列表。在JavaScript中:
ref.child('users').child(???).child('profile').on('child_added'...這不起作用,因為我們投入了什么???。Firebase操作需要能夠從一個位置讀取整個列表,并且用戶需要具有該整個位置的讀取權限(而不僅僅是對各個子節點)。
如果我們構建數據以將公共信息與私人信息分開,我們得到:
users: {
uidOfJacob: {
stackId: 884522,
ssn: "999-99-9999",
profile: {
displayName: "Jacob Philips"
}
},
uidOfPuf: {
stackId: 209103,
ssn: "999-99-9999",
profile: {
displayName: "Frank van Puffelen"
}
}},"profile": {
uidOfJacob: {
displayName: "Jacob Philips"
},
uidOfPuf: {
displayName: "Frank van Puffelen"
}}你可以用以下方法保護它:
{
"rules": {
"users": {
"$uid": {
".read": "auth.uid == $uid",
".write": "auth.uid == $uid"
}
},
"profiles": {
".read": true,
"$uid": {
".write": "auth.uid == $uid"
}
}
}}要獲取公共用戶配置文件的列表,您需要執行以下操作:
ref.child('profiles').on('child_added'...這將有效,因為每個人都有閱讀權限profiles。
添加回答
舉報
0/150
提交
取消
