我发现了以下问题:Firebase权限被拒绝,其中提到使用以下JSON代码:
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
它仅允许经过身份验证的用户访问读取或写入。这对我来说将是完美的。
唯一的问题是这个答案已经过时了。据我所知,他们不再使用JSON。
目前,我的规则如下所示:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if false;
}
}
}
如何像我在老问题中那样,仅使用提供并加载到我的应用程序中的“ API密钥”仅允许用户进行读写访问?
编辑,我现在知道Firebase实时数据库使用JSON规则,而Firebase Firestore使用实际的Firebase安全规则语言。
因此,在意识到这一点之后,我将所有规则设置为auth!= null。
在实时数据库规则中:
{
"rules": {
"users": {
".read": "auth != null",
".write": "auth != null"
}
}
}
还有云存储规则,以防万一:
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if request.auth != null;
}
}
}
我没有发现我做错了什么,我直接正确发送了API密钥,我只是为了测试而记录了它,我所有的引用都是正确的...
我不明白....
编辑:代码
const firebase = require('firebase');
var config = {
apiKey: "(Key removed... for obvious reasons)",
authDomain: "discord-trust.firebaseapp.com",
databaseURL: "https://discord-trust.firebaseio.com",
storageBucket: "discord-trust.appspot.com"
};
firebase.initializeApp(config);
... Lots of code between these two ...
case `testWriteReputation`: {
if (msg.author.bot) return;
if (msg.author.id === bot.user.id) return;
firebase.database().ref(`BasicUserData/${msg.author.id}`).set({
lastLoggedUsername: msg.author.tag,
lastLoggedAvatar: msg.author.avatar,
lastLoggedDiscriminator: msg.author.discriminator,
userAccountCreated: msg.author.createdAt,
userIdentifier: msg.author.id
});
break;
}
再次删除了api键,可以在此处找到整个javascript文件:https : //hatebin.com/egchvudbew
如文档中所述,如果您想auth
在Realtime Database Security规则中使用该变量(允许您基于每个用户控制数据访问),则需要使用Firebase Authentication。
用户认证后,“实时数据库规则”规则中的auth变量将填充用户的信息。此信息包括其唯一标识符(uid)以及链接的帐户数据(例如Facebook ID或电子邮件地址)以及其他信息。
请注意,实时数据库和Firestore是两个完全不同的Firebase服务。它们不共享相同的安全规则语法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句