Ang*_*zar 2 javascript firebase firebase-security firebase-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"
});
}
Run Code Online (Sandbox Code Playgroud)
这是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
});
}
});
}
Run Code Online (Sandbox Code Playgroud)
这些是安全规则
{
"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'"
}
}
}
Run Code Online (Sandbox Code Playgroud)
我不得不提到我正在使用Vuejs
您正在授予特定用户访问权限:/users/$uid。但是要能够对Firebase中的某个节点运行查询,您必须对该节点具有读取权限。因此Firebase拒绝您的查询,因为您无权访问/users。文档在“规则不是过滤器”部分对此进行了介绍。许多人遇到这个问题,因为这是关系数据库中非常普遍的方法。这一事实的规则是不过滤和权限向下级联,是火力地堡安全模型的两种最常见的陷阱。
在这种情况下,您可能希望授予所有用户访问整个/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",
}
}
}
Run Code Online (Sandbox Code Playgroud)
这样,每个经过github身份验证的用户都可以读取所有用户,因此查询不会被拒绝。
在其他情况下,您可能必须对数据进行不同的建模,以使用所需的安全性约束。
| 归档时间: |
|
| 查看次数: |
1091 次 |
| 最近记录: |