Search code examples
angularngrxngrx-store

Angular 6 - Resolver + Guard + ngrx


I use ngrx and i have a store that has a list of academies ['academy1', 'academy2']

I populate the store with a resolver.

Now I need to

canActivate(route: ActivatedRouteSnapshot, state_: RouterStateSnapshot): any {

  return this.store.pipe(select(state => state.user)).subscribe((user) => {
     return user.academies.includes(route.params.domain);
  });
}

But as I know... guards running before resolvers. So.... how can I check permissions with guards and ngrx?


Solution

  • On the first request, this can never work. You need to have a route which will populate the store before this guard would work if you are populating in a resolver.

    If this data is required to work out if you can activate any route, then you need to populate the store on (for example) the result of a login action, before you attempt to navigate. Alternatively, have a default route which does not have this guard but populates the store, then navigate (if you do not have a login action).