gpt4 book ai didi

angularjs - Firebase 安全规则 - 仅允许读取用户的内容

转载 作者:行者123 更新时间:2023-12-01 00:55:48 24 4
gpt4 key购买 nike

我的 Firebase 数据结构有点像这样:

+ tasks
+ task_id
+ title
+ user
+ ...
+ users
+ ...

来自前端(使用 AngularJS 和 AngularFire(加上内置的 SimpleLogin)) - 当登录用户创建任务时,用户的 uid(例如 simplelogin:2)被放入正在创建的任务的用户部分。

当用户登录时,他们只能使用以下命令查看他们的任务:
$firebase( ref.orderByChild('user').equalTo(User.uid) );

下一步是保护数据,这是我正在努力的地方。我试过了:
"tasks": {
".indexOn": ["user", "order"],
".write": "auth != null",
".read": "auth != null && data.child('user').val() == auth.uid",
"$task": {
"title": {
".validate": "newData.isString() && newData.val().length <= 1000"
},
"completed": {
},
"time_added": {
".validate": "newData.val() <= now"
},
"order": {
},
"user": {
".validate": "newData.val() != null && newData.val() == auth.uid"
},
"$other": {
".validate": false
}
}
}

并且:
"tasks": {
"$task": {
".indexOn": ["user", "order"],
".write": "auth != null",
".read": "auth != null && data.child('user').val() == auth.uid",

"title": {
".validate": "newData.isString() && newData.val().length <= 1000"
},
"completed": {
},
"time_added": {
".validate": "newData.val() <= now"
},
"order": {
},
"user": {
".validate": "newData.val() != null && newData.val() == auth.uid"
},
"$other": {
".validate": false
}
}
}

但是我得到:

Error: permission_denied: Client doesn't have permission to access the desired data.



我哪里错了?

最佳答案

tasks集合没有 user child 。每个具体任务有一个 user child 。因此,您需要将阅读规则向下移动一级:

"tasks": {
"$task_id" {
".read": "auth != null && data.child('user').val() == auth.uid",
}

另请参阅以下示例: https://www.firebase.com/docs/security/guide/user-security.html

关于angularjs - Firebase 安全规则 - 仅允许读取用户的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28159083/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com