我找到了一个使用firebase security api here
的基于权限的聊天室的简洁示例注意"chat": {
// the list of chats may not be listed (no .read permissions here)
我实际上需要在加载收件箱时列出用户所属的聊天内容,但我似乎无法正确获取.read
规则。
我尝试过使用以下规则,这些规则很有意义,但不起作用:
"convos": {
".read" : "auth != null && data.child('users').hasChild(auth.id)",
我怀疑问题是convo和用户之间仍然有一个级别..也许会更有意义:
"convos": {
".read" : "auth != null && data.child($key + '/users').hasChild(auth.id)",
$key : { ... }
但不允许抱怨$ key尚未存在。
如何让用户使用此设置提取他们所属的所有计划?
答案 0 :(得分:1)
您无法使用安全规则来过滤数据。通常,您的数据结构将完全取决于您的特定用例 - 大多数直接取决于数据的回读方式。
一般解决方案是将用户所属的聊天内容与批量聊天数据分开,即heavily denormalize,并单独访问聊天。
/messages/$chat_id/... (messages chronologically ordered using push() ids)
/chats/$chat_id/... (meta data)
/my_chats/$user_id/$chat_id/true (the value here is probably not important)
现在要访问我的所有聊天记录,我可以执行以下操作:
var fb = new Firebase(URL);
fb.child('my_chats/'+myUserId).on('child_added', function(snap) {
var chatID = snap.name());
loadChat(chatID);
});
function loadChat(chatID) {
fb.child('messages/'+chatID).on('child_added', function(snap) {
console.log('new message', chatID, snap.val());
});
}
您仍然需要安全规则来验证聊天消息的结构以及访问用户的权限。聊天列表,等。但过滤的功能可以通过这样的索引或其他创意数据结构来完成。
答案 1 :(得分:0)
我不完全确定你是如何构建Firebase的,但这可能会有效:
"convos": {
$key : {
".read" : "auth != null && data.child('users').hasChild(auth.id)",
...
}