身份验证后无法从 Firebase 读取数据
Can't read data from Firebase after authentication
大家好,我是Firebase的新手,我正在尝试开发一个简单的聊天应用程序。到目前为止,我已经按照Firebase文档中的步骤完成了身份验证。
这是我的登录方法
loginUser: function(){
console.log("Login button");
var self = this;
ref.authWithOAuthPopup("github", function(error, authData) {
if (error) { console.log("Login Failed!", error);}
else {
console.log(authData);
ref.child("users").child(authData.uid).set({
auth : authData.auth,
provider : authData.provider,
name : authData.github.displayName,
imgUrl : authData.github.profileImageURL,
token: authData.token
});
self.user.name = authData.github.displayName;
self.user.imgUrl = authData.github.profileImageURL;
self.user.provider = authData.provider;
setTimeout(function(){ self.authenticated = true; }, 2000);
this.getContacts();
}
},{
remember : "sessionOnly",
scope: "user"
});
}
这是getContacts方法(我试图控制台快照,但一无所获)
getContacts: function(){
console.log('GET CONTACTS');
var self = this;
//retrieving all the user, but for somehow this request doesn't execute
ref.child('users').once('value',function(snapshot){
var contacts = snapshot.val();
console.log(contacts);
for(var contact in contacts){
self.contacts.push({
id: contact,
name: contacts[contact].name,
imgUrl: contacts[contact].imgUrl,
provider: contacts[contact].provider
});
}
});
}
这些是安全规则
{
"rules": {
"users": {
"$uid": {
// grants write access to the owner of this user account whose uid must exactly match the key ($uid)
".write": "auth !== null && auth.uid === $uid",
// grants read access to any user who is logged in with GitHub
".read": "auth !== null && auth.provider === 'github'"
}
}
}
我不得不提一下,我正在使用 Vuejs
您正在向特定用户授予访问权限:/users/$uid
.但是,为了能够针对 Firebase 中的节点运行查询,您必须对该节点具有读取权限。因此,Firebase 会拒绝您的查询,因为您无权访问/users
。文档在"规则不是筛选器"一节中对此进行了介绍。许多人都遇到这个问题,因为这在关系数据库中是一种非常常见的方法。规则不是过滤器,权限会级联,这是 Firebase 安全模型最常见的两个缺陷。
在这种情况下,您可能希望授予所有用户对整个/users
节点的访问权限,因此您只需将读取权限上移一级:
{
"rules": {
// grants read access to any user who is logged in with GitHub
".read": "auth !== null && auth.provider === 'github'"
"users": {
"$uid": {
// grants write access to the owner of this user account whose uid must exactly match the key ($uid)
".write": "auth !== null && auth.uid === $uid",
}
}
}
有了这个,每个经过 github 身份验证的用户都可以读取所有用户,因此查询不会被拒绝。
在其他情况下,您可能必须以不同的方式对数据进行建模,以使用所需的安全约束。
相关文章:
- 从桌面读取python文件时高亮显示代码
- Javascript-如何读取json文件中的列并将其保存在Javascript数组中
- 使用Dnamics CRM 2011中的JavaScript读取子网格的所有记录,而不考虑活动页面
- 将Firebase与Electron结合使用
- Firebase迁移-简单的Firebase.set没有'不再工作了——旧的还是新的
- 节点Js:How to catch a“;没有这样的文件或目录“;读取线模块出错
- TypeError:无法读取属性'推'未定义的JavaScript
- 如何使用AngularJS从Firebase读取Object
- 如何使 Firebase 对象只写入数据而不读取
- 如何从Firebase读取数据并将其写入文本框javascript
- 从Firebase到Google图表读取Json
- 身份验证后无法从 Firebase 读取数据
- 基于数据对象的 Firebase 用户写入/读取规则
- firebase onAuth()引发:TypeError无法读取属性'auth'的未定义
- 从firebase链接应用程序中的.val()对象读取值
- 不能使用.once()函数从Firebase数据库读取数据
- Firebase启动时从数据库加载与读取新添加的内容
- 无法使用JavaScript从Firebase读取数据
- 如何在Firebase中使用js web API捕获读取权限拒绝
- Firebase-未捕获(在promise中)TypeError:无法读取属性