When configuring Access Control for my Angular app, I have structured the User object as follows: -User --firstName --phoneNumber --posts[] <--- String array of post id's
getPosts(): Observable<any[]> {
return this.postsCollection.snapshotChanges().pipe(
map((actions) => {
return actions.map((a) => {
const data = a.payload.doc.data();
const data2 = { id: a.payload.doc.id, ...data};
this.hasPostAccess(data2);
return { id: a.payload.doc.id, ...data};
});
})
);
}
private hasPostAccess(data: any) {
const inspect = obj => {
for (const prop in obj) {
if (obj.hasOwnProperty(prop)) {
// console.log(`${prop}: ${obj[prop]}`);
if (prop === 'id') {
console.log(obj[prop]);
const PostId = obj[prop];
const currentUser = JSON.stringify(localStorage.getItem('user'));
if (currentUser.search(PostId) === -1) {
console.log('Denied: ' + PostId);
} else {
console.log('Allowed: ' + PostId);
}
console.log('Current USER: ' + JSON.stringify(currentUser));
// if (currentUser(PostId) > -1) {
// console.log('Allowed: ' + PostId);
// } else {
// console.log('Denied: ' + PostId);
// }
}
}
}
};
inspect(data);
}
To display only the posts associated with the user, I need the getPosts Observable to filter and return the post ids stored in the User's post array when subscribed.