-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';
constructor(
private router: Router,
private route: ActivatedRoute,
+ private zone: NgZone,
private net: EgNetService,
private auth: EgAuthService
) {}
this.router.events.subscribe(routeEvent => {
if (routeEvent instanceof NavigationEnd) {
//console.debug(`EgStaffComponent routing to ${routeEvent.url}`);
- this.basicAuthChecks(routeEvent);
+ this.basicAuthChecks(routeEvent.url);
}
});
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}) => {
* 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]);