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
提交
取消
