>我设置了以下 firestore 规则,我想让任何人在建议集合中创建文档,但只有特定用户可以更新它。规则如下所示:
service cloud.firestore {
match /databases/{database}/documents {
match /suggestions/{sugg} {
allow create, read;
allow update: if request.auth.uid == 'abc123';
}
}
}
当我在提供的模拟器中对此进行测试时,它工作正常;但是等待 30 分钟后,我在部署的应用程序中进行测试,并收到错误:
错误:缺少权限或权限不足。
在我的应用程序中,我正在对建议集合进行add()
调用。所以在我的规则中,我指定allow create
,这应该就足够了。我添加了read
以防返回的文档算作读取。
客户:(角度火(
this.afs.collection('suggestions').add(sugg).then(() => {
this.submitted = true;
}, err => console.error('Firebase error:', err));
问题是当request.auth为空时,Firestore无法尝试匹配uid
。解决方案是添加一些功能:
service cloud.firestore {
match /databases/{database}/documents {
function isSignedIn() {
return request.auth != null;
}
function isAdmin() {
return isSignedIn() && request.auth.uid == 'abc123';
}
match /suggestions/{sugg} {
allow create, read;
allow update: if request.auth.uid == 'abc123';
}
}
}