1 import {Component, OnInit, NgZone, HostListener} from '@angular/core';
2 import {Router, ActivatedRoute, NavigationEnd} from '@angular/router';
3 import {AuthService, AuthWsState} from '@eg/core/auth.service';
4 import {NetService} from '@eg/core/net.service';
5 import {AccessKeyService} from '@eg/share/accesskey/accesskey.service';
6 import {AccessKeyInfoComponent} from '@eg/share/accesskey/accesskey-info.component';
8 const LOGIN_PATH = '/staff/login';
9 const WS_BASE_PATH = '/staff/admin/workstation/workstations/';
10 const WS_MANAGE_PATH = '/staff/admin/workstation/workstations/manage';
13 templateUrl: 'staff.component.html',
14 styleUrls: ['staff.component.css']
17 export class StaffComponent implements OnInit {
20 private router: Router,
21 private route: ActivatedRoute,
23 private net: NetService,
24 private auth: AuthService,
25 private keys: AccessKeyService
30 // Fires on all in-staff-app router navigation, but not initial
32 this.router.events.subscribe(routeEvent => {
33 if (routeEvent instanceof NavigationEnd) {
34 // console.debug(`StaffComponent routing to ${routeEvent.url}`);
35 this.preventForbiddenNavigation(routeEvent.url);
39 // Redirect to the login page on any auth timeout events.
40 this.net.authExpired$.subscribe(expireEvent => {
42 // If the expired authtoken was identified locally (i.e.
43 // in this browser tab) notify all tabs of imminent logout.
44 if (!expireEvent.viaExternal) {
45 this.auth.broadcastLogout();
48 console.debug('Auth session has expired. Redirecting to login');
49 this.auth.redirectUrl = this.router.url;
51 // https://github.com/angular/angular/issues/18254
52 // When a tab redirects to a login page as a result of
53 // another tab broadcasting a logout, ngOnInit() fails to
54 // fire in the login component, until the user interacts
55 // with the page. Fix it by wrapping it in zone.run().
56 // This is the only navigate() where I have seen this happen.
58 this.router.navigate([LOGIN_PATH]);
62 this.route.data.subscribe((data: {staffResolver: any}) => {
63 // Data fetched via StaffResolver is available here.
70 * Prevent the user from leaving the login page when they don't have
73 * Prevent the user from leaving the workstation admin page when
74 * they don't have a valid workstation.
76 * This does not verify auth tokens with the server on every route,
77 * because that would be overkill. This is only here to keep
78 * people boxed in with their authenication state was already
79 * known to be less then 100%.
81 preventForbiddenNavigation(url: string): void {
83 // No auth checks needed for login page.
84 if (url.startsWith(LOGIN_PATH)) {
88 // We lost our authtoken, go back to the login page.
89 if (!this.auth.token()) {
90 this.router.navigate([LOGIN_PATH]);
93 // No workstation checks needed for workstation admin page.
94 if (url.startsWith(WS_BASE_PATH)) {
98 if (this.auth.workstationState !== AuthWsState.VALID) {
99 this.router.navigate([WS_MANAGE_PATH]);
104 * Listen for keyboard events here -- the root directive -- and pass
105 * events down to the key service for processing.
107 @HostListener('window:keydown', ['$event']) onKeyDown(evt: KeyboardEvent) {
112 @ViewChild('egAccessKeyInfo')
113 private keyComponent: AccessKeyInfoComponent;