Seeking guidance on implementing firebase rules and queries for Firestore in a Vue.js application.
Requirement: User must be authenticated
- User can
read/write
their own data entries - User can
read
data with"visibility"
field set to"public"
- User can read specific entries using document ID, even if
"visibility"
is not public (e.g."private"
)
Assume the collection contains entries created by different users:
- User-B can
read/write
the 2nd entry - User-A, B, C can
read
1st and 3rd entries - User-A, C can
read
2nd entry with known document ID
Is "user_id"
field required to filter self-created documents or does Firebase provide built-in functionality based on authentication?
[
{
"user_id": "User-A's firebase id",
"foo": "foo",
"bar": true,
"visibility": "public"
},
{
"user_id": "User-B's firebase id",
"foo": "foo",
"bar": true,
"visibility": "private"
},
{
"user_id": "User-C's firebase id",
"foo": "foo",
"bar": true,
"visibility": "public"
}
]
Requesting assistance with creating rules and queries using this npm package.
Current query for retrieving self-created entries:
this.db.collection("my_collection").where('user_uid','==',this.$store.state.user.id).get().then((querySnapshot) => {
querySnapshot.forEach((doc) => {
//do something
});
});
Create entry query:
db.collection("my_collection").add({
user_uid: this.$store.state.user.id,
foo: this.foo,
bar: this.bar,
visibilty: this.visibility
})
Existing rule:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth.uid != null;
}
}
}