[英]Getting permission error when creating a Firestore document
I'm trying to securely create a Firestore document without requiring user authentication and have defined the following rule to do so:我试图在不需要用户身份验证的情况下安全地创建 Firestore 文档,并为此定义了以下规则:
allow create: if request.resource.data.deviceid == resource.id;
According to what I've read from Google, this should allow the document to be created if the pending write includes a field called 'DeviceID' that matches the document ID, which is what I think I'm doing (I confirmed that androidID is identical to the document ID):根据我从谷歌上读到的内容,如果待处理的写入包含与文档 ID 匹配的名为“DeviceID”的字段,这应该允许创建文档,这就是我认为我正在做的(我确认 androidID 是与文档 ID 相同):
data.put("DeviceID", androidID);
mDevIDDocRef.set(data)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "DocumentSnapshot successfully written!");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "Error writing document", e);
}
});
However, the above fails with a permission exception.但是,上述操作因权限异常而失败。 What am I doing wrong?
我究竟做错了什么?
During the create rule evaluation, the resource does not exist yet, so you have to read its future value: request.resource.data.whatever在创建规则评估期间,资源尚不存在,因此您必须读取其未来值:request.resource.data.whatever
To access an automatically generated document id, you need to read it from the document path:要访问自动生成的文档 ID,您需要从文档路径中读取它:
match /myCollection/{docId} {
allow create: if request.resource.data.deviceid == docId;
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.