Javascript 仅访问自有数据、公共数据或特定数据的Firebase规则
我希望通过firebase规则和firebase查询实现以下功能。(我正在使用firestore和vuejs) 前提条件:用户在任何情况下都必须进行身份验证登录Javascript 仅访问自有数据、公共数据或特定数据的Firebase规则,javascript,firebase,vue.js,google-cloud-firestore,firebase-security,Javascript,Firebase,Vue.js,Google Cloud Firestore,Firebase Security,我希望通过firebase规则和firebase查询实现以下功能。(我正在使用firestore和vuejs) 前提条件:用户在任何情况下都必须进行身份验证登录 允许用户读取/写入他自己创建的所有数据条目 允许用户读取所有字段设置为“public” 即使条目的“可见性”字段设置为不相等的“公共”(例如“私有”),如果用户具有条目的id,则允许用户读取特定条目 让我们假设这是我收藏的条目 第一个条目由用户A创建,第二个条目由用户B创建,第三个条目由用户C创建: 因此: 应允许用户B读取/写入第二个
读取/写入他自己创建的所有数据条目
读取所有字段设置为“public”
“可见性”
字段设置为不相等的“公共”
(例如“私有”
),如果用户具有条目的id
,则允许用户读取特定条目读取/写入第二个条目(其他用户依此类推)
读取第一个和第三个条目
读取第二个条目。比如db.collection('my_collection').doc('abcxyz').get()
“user\u id”
来根据用户创建的文档过滤请求?或者firebase是否具有基于身份验证检索自创文档的内置功能
有人能帮我创建规则和请求吗?(我正在使用此npm)
这是我现在用来检索自己创建的条目的查询
this.db.collection("my_collection").where('user_uid','==',this.$store.state.user.id).get().then((querySnapshot) => {
querySnapshot.forEach((doc) => {
//do something
});
});
这是用来创造的
db.collection("my_collection").add({
user_uid: this.$store.state.user.id,
foo: this.foo,
bar: this.bar,
visibilty: this.visibility
})
这是我到现在为止的规则
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth.uid != null;
}
}
}
Firestore不会自动将文档与创建文档的用户关联。如果您需要这样的元数据,您需要像现在一样将其存储在每个文档中 要确保用户只能阅读自己的文档,您需要两件事:
为了允许其他用例,您需要以类似的方式扩展规则以允许访问。但您需要为每个案例执行单独的查询,因为Firestore查询不能包含或键入多个字段上的条件,而这正是您的用例所需要的。非常感谢您
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth.uid != null;
}
}
}
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth != null && request.auth.uid == resource.data. user_uid;
}
}
}