【发布时间】:2020-07-13 12:11:13
【问题描述】:
我正在尝试在 Firestore 中设置规则,如果每个人都通过了应用程序的身份验证,则每个人都可以读取彼此的内容,但只有文档的所有者才能创建、写入、更新或删除它们。
我在 Firestore 中设置了以下规则:
rules_version = '2';
service cloud.firestore {
match /databases/{database} {
match /codes/{userID} {
allow create, write, update, delete: if request.auth.uid == userID;
allow read: if request.auth.uid != null;
}
}
}
我的 Firestore 结构是:
Collection 'codes' > Document 'BNhBibYZ0ThCNCH2gzPRufFsIk22' > nothing in here
我的要求是:
firestore()
.collection('codes')
.doc(this.state.userID)
.onSnapshot((querySnapshot) => {
if (!querySnapshot) {
let testObj = {"hello": "world"}
firestore().collection('codes').doc(this.state.userID).set(testObj);
}
});
我收到的输出是:Error: [firestore/permission-denied] The caller does not have permission to execute the specified operation.
我做错了什么?
【问题讨论】:
-
查询时
this.state.userID的确切值是多少? (您确定要使用onSnapshot()而不是get()?) -
鉴于您的代码结构,我相当确定失败的是
onSnapshot,这意味着从 Firestore 的角度来看,您没有经过身份验证。您能否再次检查firebase.auth().currentUser是否为非空且currentUser.uid与this.state.userID匹配? -
@DougStevenson 我可以确认 this.state.userID 是
BNhBibYZ0ThCNCH2gzPRufFsIk22,但以防万一我也尝试对其进行硬编码。问题仍然存在。我用get()替换了onSnapshot(),但它仍然是一样的。 @MichaelBleigh 在 set 函数之前,我已经打印出 auth().currentUser.uid 并且它与文档的名称相匹配,即BNhBibYZ0ThCNCH2gzPRufFsIk22。我尝试只保留.set函数,但仍然存在权限问题。
标签: javascript firebase google-cloud-firestore firebase-security