【发布时间】:2019-07-22 10:27:33
【问题描述】:
我正在尝试保护用户可以管理其乐队的应用。用户可以是多个乐队的一部分,因此可以根据他们所在的乐队拥有不同的角色。角色存储在乐队文档的地图字段中。
我目前有以下 firebase/firestore 规则设置:
service cloud.firestore {
match /databases/{database}/documents {
function isSignedIn() {
return request.auth != null;
}
match /bands/{bandID}{
function isBandAdmin(userID){
// Determine if user is a band admin
return get(/databases/$(database)/documents/bands/$(bandID)).data.members[userID].role == 'Admin';
}
allow read, update, create: if isSignedIn() && isBandAdmin(request.auth.uid);
// Rules for a band's concerts
match /events/{event}{
allow read, update, create: if isSignedIn() && (isBandAdmin(request.auth.uid));
}
}
}
现在,如果我在 firebase 控制台提供的模拟器中执行 get 请求,这将完美运行。但在我的项目中,我正在尝试执行以下请求:
database.collection("bands").where("name" ,"==", "AC/DC").get().then((docs) => {...});
这给了我一个权限错误。我怎样才能做到这一点?
PS:我已经发布了一个类似的问题,但这是一个重要的更新。
【问题讨论】:
-
用户似乎没有登录,或者不是管理员。鉴于您提供的信息,很难说它是哪一个。要确定用户是否已登录,请在运行查询之前登录
firebase.auth().currentUser。 -
这不是问题所在。用户已登录,也是管理员。下面的答案是正确的。
标签: javascript firebase google-cloud-firestore firebase-security