Whisher
Whisher

Reputation: 32726

Ngrx two guards for a route at logout the second doesn't trigger

I've got these routes:

{
    path: 'admin',
    canLoad: [AuthGuard, AccountGuard],
    loadChildren: () =>
      import('./features/admin/admin.module').then(m => m.AdminModule),
    data: { preload: false }
  },
  {
    path: 'auth',
    loadChildren: () =>
      import('./features/auth/auth.module').then(m => m.AuthModule),
    data: { preload: false }
  }

At login, both are triggered but when in the admin area I do logout deleting both auth and account states, redirecting to the auth route if I do login again without refresh only the first is triggered. I've tried using

onSameUrlNavigation: 'reload'

runGuardsAndResolvers: 'always'

but I still have the same behavior.

Any ideas?

NB I've also tried putting a simple console.log in the AccountGuard like

canLoad(): Observable<boolean> {console.log('Account)}

but in the second login I don't see any message in the dev tools

Upvotes: 0

Views: 239

Answers (1)

Hemendra
Hemendra

Reputation: 388

As per the definition from angular website - "CanLoad is an Interface that a class can implement to be a guard deciding if children can be loaded".

When the route/child is loaded the CanLoad guard returns true and then, once loaded, the guard will not be called again. It's the role of CanActivate to do these kind of checks everytime you are navigating to a route. I would suggest to implement CanActivate and assign the guard which needs to be run on login.

Also here's the link to github where this feature has been implemented by angular team - https://github.com/angular/angular/commit/8785b2bf6db8c3ecc4bd5edcf667d5f2f53f5271

Upvotes: 1

Related Questions