title: how to authenticate and authorize at each route level in angular 4 with keycloak
body: <p>I have integrated Keycloak with our application.Login and Logout flow is working properly except one scenario.I wanted to authenticate then authorize each route. Because when user clears the browser session or clear session from admin console of keycloak, when user tries to access other route it says 401 since I have written AppGuard. I have tried to check getKeycloakInstance() it returns same object when has valid session and after session expires also. Then I tried to call init(config) from canActive() of AuthGuard it was looping continuously. How to authenticate at route level.</p>
  
  <h2>app-routing.module.ts</h2>
  
  <pre><code>{
      path: '',
      canActivate: [AppAuthGuard],
      data: {
              path:'/idam/*'
      },
      loadChildren: './idam/idam.module#IdamModule'
  },
  </code></pre>
  
  <h2>app-auth-guard.ts</h2>
  
  <pre><code>canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise&lt;boolean&gt; {
      if (!this.keycloakService.getIsClientSecretKeyFetch()) {
        this.errorHandler._enlivenErrorhandler.handleError({"status":"401","message" :"Unable to get client details. Please contact your administrator ","url":window.location.href});
        this.router.navigate(['404'], { queryParams: { tenantId: this.cookieService.get('tenantId') } });
      }
      return super.canActivate(route, state);
    }
  
    isAccessAllowed(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise&lt;boolean&gt; {
      return new Promise((resolve, reject) =&gt; {
  
        if (!this.authenticated) {
          this.keycloakAngular.login();
          return;
        }
  
        const path = route.data.path;
        this.keycloakService.isAuthorized(path).then(
          (res) =&gt; {
            let access = res;
            if (access) {
              resolve(true);
            } else {
              resolve(false);
            }
          });
        resolve(true);
      });
    }
  </code></pre>