【发布时间】:2020-07-22 16:24:32
【问题描述】:
我知道已经存在 answer to this question on StackOverflow,但我想我的用例有点不同。
我在 firestore 上有两个集合:“用户”和“模板”
每次创建用户时,我都会创建一个模板并将模板 id 分配给相同的用户 id,如下所示。
** Repository.js **
async createUser(email, password) {
const template = await getLocalTemplate()
const credentials = await auth.createUserWithEmailAndPassword(email, password)
await db.collection('users').doc(credentials.user.uid).set({ email })
await db.collection('templates').doc(credentials.user.uid).set({
...template,
})
return credentials
}
但是,我不知道如何允许用户只写入其模板。
这些是我的 Firestore 规则:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /users/{user} {
allow create
allow read, write: if request.auth.uid == user
}
match /templates/{template} {
allow read, create
allow write: if request.auth.uid == /users/{user}
}
}
}
【问题讨论】:
标签: javascript firebase google-cloud-firestore firebase-security