0

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

Whisher
  • 31,320
  • 32
  • 120
  • 201
  • Put a whole code please. What exactly you have in guards. What kind of authentication you using ? – Mises Nov 06 '19 at 08:17

1 Answers1

1

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

Hemendra
  • 378
  • 5
  • 13
  • I can see the point, but when I do log out I reset the state therefore the guard is false and it's triggered when I do log in so I dont se why the second one is not trigered again – Whisher Nov 07 '19 at 13:11
  • please provide the code for the guards and login/logout. Even better if you can create a sample on stackblitz, it'll help in understanding what exactly is going wrong. – Hemendra Nov 08 '19 at 12:11