gpt4 book ai didi

javascript - $uid 节点如何处理 firebase-realtime-database 规则?

转载 作者:行者123 更新时间:2023-11-29 20:35:20 25 4
gpt4 key购买 nike

我正在 ionic 上开发 firebase-realtime-database,我对如何在 $uid 节点内“读”和“写”有点困惑。我目前正在处理拒绝访问的问题。

节点看起来像这样

rules {
"info": {
"$uid": {
".read": true,
".write": "$uid === auth.uid"
}
}
}

虽然我的 json 数据库看起来像这样


{
info: {
01: {
name:jay
age:16
}
}
}


我有点搞不清楚为了访问 $uid 节点内的数据需要采取哪些步骤。我确实尝试将我的 json 数据库节点更新为此。


{
info: {
f3de4734-8fb2-42bd-971d-8e693f8aab3b: { // auth.uid of dummy@gmail.com
01: {
name:jay
age:16
}
}
}
}

和我的“getafdData() 函数”路径目录到“'info/'+ getuser”。

只有当“getuser”是文件的所有者或保存数据的节点时,以上更新才有效,但是我希望它也被其他经过身份验证的用户读取,而我无法用 $uid 理解这一点。

我已经使用 firebase 的“signInWithEmailAndPassword()”函数对登录进行了身份验证

这是我用来验证的代码

 try {
var r = await this.fAuth.auth.signInWithEmailAndPassword(
"dummy@gmail.com",
"123456"
);
if (r) {
console.log("Successfully logged in!");
await this.getafdData(); \\\ get the data when login

}

} catch (err) {
console.error(err);
}
async getafdData(){

var firebaseRef = this.afd.database.ref();
let data = await new Promise(function(resolve,reject){
return firebaseRef.child('info/')
.on("child_added", function(snapshot) {
console.log(snapshot.val());
//resolve(data);


}, function(err) {
console.log(err);
reject(err);
})
});
return data;
}

你能告诉我做错了什么吗?或者更确切地说,为了访问 $uid 节点我应该执行正确的步骤?

最佳答案

".read": true 规则允许任何用户从 /info/$uid 读取数据 如果他们知道其数据的用户的 UID他们想阅读。然而,在您的代码中,您将监听器附加到 /info 并且没有人对该节点具有读取权限,因此读取被拒绝。

如果你希望每个人都能够一次读取所有用户节点,你应该将 ".read": true, 规则移动到 /info 级别你的规则:

{
"rules": {
"info": {
".read": true,
"$uid": {
".write": "$uid === auth.uid"
}
}
}

实际上,既然您说要允许其他经过身份验证的用户读取用户信息,那可能应该是:

{
"rules": {
"info": {
".read": "auth != null",
"$uid": {
".write": "$uid === auth.uid"
}
}
}

关于javascript - $uid 节点如何处理 firebase-realtime-database 规则?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56880148/

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