【问题标题】:Firebase Auth rule read if exists in subcollection not working如果子集合中存在,则读取 Firebase 身份验证规则不起作用
【发布时间】:2020-09-02 02:18:26
【问题描述】:

只有当用户 uid 是子集合成员中的文档时,我才尝试读取氏族,但我似乎无法让它工作,只是出现更多权限错误。

match /clans/{clanName} {
    allow create;
    allow read: if exists(/databases/$(database)/documents/clans/{clanName}/members/$(request.auth.uid))
            match /members/{uid} {
            allow create,write,delete: if uid == request.auth.uid;
    }
}

调用它的代码使用存储在另一个文档中的引用

this.getProfile
    .data()
    .clan.get());

相当于

firebase
    .firestore()
    .collection("clans")
    .doc(clanName)
    .get()

leaveClan() {
  console.log(this.getUser.displayName + " leaving " + this.getProfile);
  console.log(this.getProfile.data().clan.get().collection("members")
    .doc(this.getUser.uid));
  this.getProfile
    .clan
    .get()
    .collection("members")
    .doc(this.getUser.uid)
    .delete()
    .then(() => {
      firebase
        .firestore()
        .collection("profiles")
        .doc(this.getUser.uid)
        .update({
          clan: null
        });
    });
},

【问题讨论】:

  • 如果没有看到以下内容,就不可能说出为什么这些规则对您不起作用:1) 重现错误的最小代码,2) 代码或规则访问的数据。请编辑您的问题以包含此信息。我总是推荐阅读how to create a minimal, complete, verifiable example,因为遵循那里的指导可以帮助您最大限度地提高别人可以帮助您的机会。
  • 添加调用代码。
  • 谢谢。剩下的 #2:代码和规则访问的数据。请添加规则正在阅读的文档的屏幕截图。 3) 一定要显示clanName 的值,否则我们无法知道。我总是建议在此查询之前仅使用console.log(clanName),并在问题中包括更新的代码及其输出。
  • 引用的promise不是已经有文档了吗?我希望因为参考中有氏族名称,所以它会知道。我不保留氏族名称,我保留对氏族文件的引用。您可以在profiles => uid => clan: /clans/auth 下查看哪些引用clans => auth check。我希望用户只有通过 uid 出现在成员子集合中时才能阅读氏族文档。
  • 您的代码正在执行.doc(clanName)。按照我的要求记录值可确保它具有您期望的值,并且我们可以帮助您验证这一点。您还需要以相同的方式记录 UID,以便我们都可以检查它是否与您在数据库中的内容相匹配。

标签: firebase vue.js google-cloud-firestore firebase-authentication firebase-security


【解决方案1】:

您在get() 调用中嵌入{clanName} 的方式不正确。变量都采用$(variablename) 格式,类似于您已有的$(database)$(request.auth.uid)

比如:

if exists(/databases/$(database)/documents/clans/$(clanName)/members/$(request.auth.uid))

另见:

【讨论】:

    猜你喜欢
    • 2020-07-16
    • 2019-03-31
    • 2018-03-12
    • 2016-06-08
    • 2018-03-30
    • 2020-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多