I have a 'Teams' collection in Firestore. A team document will include a team_users
map field where userID: true
if they are a part of that team.
I have a query in a service that returns and displays all team documents where the logged in user id matches an ID within the team_users
field.
getTeamsList(user_id: string) {
this.teamsCollection = this.afs.collection<any>(`teams`, ref => ref.where(`team_users.${user_id}`, "==", true))
this.teams = this.teamsCollection.snapshotChanges().pipe(
map(actions => actions.map(a => {
const data = a.payload.doc.data();
const id = a.payload.doc.id;
return { id, ...data };
}))
).pipe(shareReplay());
}
What I need to achieve is a route guard that performs the following:
- Prevents a user accessing a team they don't belong to / use to belong to. For example, if the user has a bookmark for a document whilst they were in
Team A
, but have since left, they should not be able to access this route and should instead be redirected back to the url of/teams
- If the user does not belong to any teams (the collection query doesn't produce any results), then no routes are accessible a part from the
/teams
listing url
Note: A user can be a part of multiple teams. If the user was a part of Team A, B and C and has bookmarks for pages in Team A, but has since left, the route guard should prevent them from accessing any pages that were a part of Team A, but still allow access to Team B and C pages.
Does this mean that:
- Every URL within my app will need this guard?
- That every URL within my app will need to contain the team ID?
Any help would be greatly appreciated.