【问题标题】:Firestore rules don't seem to work ERROR Error: Missing or insufficient permissionsFirestore 规则似乎不起作用 ERROR 错误:缺少权限或权限不足
【发布时间】:2018-02-19 15:03:19
【问题描述】:

我在这里有一个非常简单的查询(使用 AngularFire 与 firestore 交互):

initializeEventFeed(): void {
    this.eventCollection = this.db.collection('Events', ref => ref.orderBy('date'));
    this.eventCollection$ = this.eventCollection.valueChanges();
  }

当我的规则设置为:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write;
    }
  }
}

但是如果我把规则再复杂一点,我就会得到权限不足的错误:

service cloud.firestore {
  match /databases/{database}/documents {
    match /Events/{event} {
      allow read, write;
    }
  }
}

或者更实用的东西(我想要):

service cloud.firestore {
  match /databases/{database}/documents {
    match /Events/{event} {
      allow read: if request.auth.uid != null;
      allow write: if request.auth.token.email == "authedWriteUser@gmail.com";
    }
  }
}

真正奇怪的是,即使只是将它们设置为 true,仍然会出现权限错误:

service cloud.firestore {
  match /databases/{database}/documents {
    match /Events/{event} {
      allow read: if true;
      allow write: if true;
    }
  }
}

【问题讨论】:

    标签: javascript google-cloud-firestore angularfire2 firebase-security


    【解决方案1】:

    我刚刚尝试重现您的问题,但对我来说效果很好。

    我的数据:

    我的规则:

    match /48868872/{event} {
      allow read
    }
    

    我的代码:

    var query = firebase.firestore().collection("/48868872").orderBy('date');
    
    query.get().then((snapshot) => {
      snapshot.forEach((document) => { console.log(document.id); })
    }).catch(console.error);
    

    直播链接:https://jsbin.com/kataduw/edit?html,js,console

    【讨论】:

    • 我能想到的唯一区别是你的 bin 只是原始 js,而我使用 angularfire 与 firestore 交互。 (我将编辑我的问题以提及我正在使用它,我认为这不相关)。
    • 我不认为 AngularFire 在这里会有所作为。您能否像我所做的那样在 jsbin 中重现该问题,以便我们同时查看工作和非工作示例?
    • 使用我的 jsbin,它似乎可以按预期工作。如果我添加一条规则以仅允许特定电子邮件阅读它,我将按预期收到insufficient permissions 错误。我现在想知道是否与我的查询有关,如果 request.auth 没有与它一起发送?我只是在我的应用程序中使用 google oauth,所以我认为 angularfire/firestore 正在为我处理这个问题。你知道firebase控制台中是否有任何日志可以给出更清晰的错误?
    • 这可能是,例如,如果您在用户被正确验证之前发送查询。不过,从您到目前为止共享的代码中无法判断。
    • 此时用户肯定已经通过了身份验证。在测试规则时,我正在刷新我的应用程序,并且已经登录了经过身份验证的用户。我的应用程序上的每个页面都受到路由器保护的保护,该路由器保护检查身份验证,否则它们将被重定向到登录。
    猜你喜欢
    • 2020-09-01
    • 1970-01-01
    • 2020-10-27
    • 1970-01-01
    • 2018-04-25
    • 2019-02-26
    • 2018-08-14
    • 2018-12-06
    • 1970-01-01
    相关资源
    最近更新 更多