I am trying to set up rules in Firestore where everyone can read from each other's content if they are authenticated into the application but only owners of documents can create, write, update or delete them.
I have set up the following rules in Firestore:
rules_version = '2';
service cloud.firestore {
match /databases/{database} {
match /codes/{userID} {
allow create, write, update, delete: if request.auth.uid == userID;
allow read: if request.auth.uid != null;
}
}
}
My Firestore structure is:
Collection 'codes' > Document 'BNhBibYZ0ThCNCH2gzPRufFsIk22' > nothing in here
My request is:
firestore()
.collection('codes')
.doc(this.state.userID)
.onSnapshot((querySnapshot) => {
if (!querySnapshot) {
let testObj = {"hello": "world"}
firestore().collection('codes').doc(this.state.userID).set(testObj);
}
});
The output I receive is: Error: [firestore/permission-denied] The caller does not have permission to execute the specified operation.
What am I doing wrong?