LP#626157 ang2 logout/auth repairs user/berick/lp1626157-ang2-sandbox
authorBill Erickson <berickxx@gmail.com>
Thu, 28 Dec 2017 21:26:53 +0000 (16:26 -0500)
committerBill Erickson <berickxx@gmail.com>
Thu, 28 Dec 2017 21:26:53 +0000 (16:26 -0500)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/eg2-src/src/app/staff/app.component.ts

index 788edc2..f4bf36f 100644 (file)
@@ -1,4 +1,4 @@
-import {Component, OnInit} from '@angular/core';
+import {Component, OnInit, NgZone} from '@angular/core';
 import {Router, ActivatedRoute, NavigationEnd} from '@angular/router';
 import {EgAuthService, EgAuthWsState} from '@eg/core/auth';
 import {EgNetService} from '@eg/core/net';
@@ -16,6 +16,7 @@ export class EgStaffComponent implements OnInit {
     constructor(
         private router: Router,
         private route: ActivatedRoute,
+        private zone: NgZone,
         private net: EgNetService,
         private auth: EgAuthService
     ) {}
@@ -28,7 +29,7 @@ export class EgStaffComponent implements OnInit {
         this.router.events.subscribe(routeEvent => {
             if (routeEvent instanceof NavigationEnd) {
                 //console.debug(`EgStaffComponent routing to ${routeEvent.url}`);
-                this.basicAuthChecks(routeEvent);
+                this.basicAuthChecks(routeEvent.url);
             }
         });
 
@@ -41,7 +42,16 @@ export class EgStaffComponent implements OnInit {
 
             console.debug('Auth session has expired. Redirecting to login');
             this.auth.redirectUrl = this.router.url;
-            this.router.navigate([this.loginPath]);
+
+            // https://github.com/angular/angular/issues/18254
+            // When a tab redirects to a login page as a result of
+            // another tab broadcasting a logout, ngOnInit() fails to
+            // fire in the login component, until the user interacts
+            // with the page.  Fix it by wrapping it in zone.run().
+            // This is the only navigate() where I have seen this happen.
+            this.zone.run(() => {
+                this.router.navigate([this.loginPath]);
+            });
         });
 
         this.route.data.subscribe((data: {staffResolver : any}) => {
@@ -58,17 +68,17 @@ export class EgStaffComponent implements OnInit {
      * workstation, respectively, once the initial route resolvers have
      * done their jobs.
      */
-    basicAuthChecks(routeEvent: NavigationEnd): void {
+    basicAuthChecks(url: string): void {
 
         // Access to login page is always granted
-        if (routeEvent.url == this.loginPath) return;
+        if (url == this.loginPath) return;
 
         if (!this.auth.token()) 
             this.router.navigate([this.loginPath]);
 
         // Access to workstation admin page is granted regardless
         // of workstation validity.
-        if (routeEvent.url.indexOf(this.wsAdminBasePath) >= 0) return;
+        if (url.indexOf(this.wsAdminBasePath) >= 0) return;
 
         if (this.auth.workstationState != EgAuthWsState.VALID)
             this.router.navigate([this.wsAdminBasePath]);