0

canLoad Is Firing Twice On Rejection or Even In Acceptance

export const ROUTES: Routes = [
    {
        path: 'Inbox',
        canLoad: [CanActivateViaAuthGuardService],
        loadChildren: 'LazyLoaded module',
    }
]

canLoad Implementation

canLoad(route: Route): Observable<boolean> {
    console.log("CanLoad Is Called?");

    return this._apsm.IsModuleCanbeLoaded()
      .map(
      res => {
        console.log(res);

        if (res === false) {
          // User:Don't Has Access
          this.router.navigateByUrl('/error');

          return false;
        }

        // User Has Access:
        return true;
      })
      .catch((error: any) => {
        console.log(error);
        this.router.navigateByUrl('/error');
        return Observable.of(false);
      });    
  }

Servie Method Invoked

Assuming Observale Boolean Response Will be returned

IsModuleCanbeLoaded(): Observable<boolean> {         
    return false as Observable<boolean>;
}
pim
  • 12,019
  • 6
  • 66
  • 69
khizer
  • 1,242
  • 15
  • 13
  • Do either of your `router.navigateByUrl` calls take the user "outside" of the module? You have to be SUPER careful using redirects within route guards for this very reason. It's easy to create loops. Definitely not saying you shouldn't do it, just make sure there aren't any loops. – joshrathke Aug 18 '17 at 15:54
  • You could try turning on route tracing to see if it provides any clues. Add ` { enableTracing: true }` to your RouterModule.forRoot call. – DeborahK Aug 18 '17 at 20:48
  • more descriptive title, corrected code indenting and syntax errors – pim Aug 19 '17 at 13:14
  • I have the same issue, how did you fix it? – Murhaf Sousli Oct 11 '20 at 22:11

0 Answers0