I'm trying to build a similar like the role-based access (https://firebase.google.com/docs/firestore/solutions/role-based-access) within my firestore database. But the roles like I defined them are not working. I'm always getting the error with
Missing or insufficient permissions.
What I have so far is the following in my firestore rules:
service cloud.firestore {
match /databases/{database}/documents {
function isSignedIn() {
return request.auth != null;
}
function getRole(rsc) {
return rsc.data.roles[request.auth.uid];
}
function isOneOfRoles(rsc, array) {
return isSignedIn() && (getRole(rsc) in array) || isSignedIn() && rsc.data.openWorld == true;
}
match /users/{user} {
allow read: if isSignedIn() && request.auth.uid == resource.data.uid;
}
// Match any document in the 'worlds' collection
match /worlds/{world} {
allow read: if isOneOfRoles(resource, ['owner', 'writer', 'commenter', 'reader']);
}
}
}
My document structure looks like: ROOT/worlds/{WORLDID}/... and every document in there like the following:
{
name: "Open World",
desc: "",
openWorld: true,
roles: {
DzpqsN6QjmZoCoM0eymWJ17VKbG3: "owner"
}
}
I'm using this with an Angular Frontedn and Angularfire with the following code which is wrapped into a service:
getWorlds(userId): Observable<any> {
return this.afs.collection('worlds').snapshotChanges().map(actions => {
return actions.map(a => {
const data = a.payload.doc.data();
const id = a.payload.doc.id;
return { id, ...data };
});
}).catch((e: any) => Observable.throw(this.errorHandler(e)));
}
Can anybody see where there might be an error in there or is there a general bug with firestore at this point? Thanks!