1 import {Component, OnInit, Renderer2} from '@angular/core';
2 import {Location} from '@angular/common';
3 import {Router, ActivatedRoute} from '@angular/router';
4 import {AuthService, AuthWsState} from '@eg/core/auth.service';
5 import {StoreService} from '@eg/core/store.service';
6 import {OrgService} from '@eg/core/org.service';
7 import {OfflineService} from '@eg/staff/share/offline.service';
9 // Direct users to the AngJS splash page when no routeTo is provided.
10 const SPLASH_PAGE_PATH = '/eg/staff/splash';
13 templateUrl : './login.component.html'
15 export class StaffLoginComponent implements OnInit {
29 private router: Router,
30 private route: ActivatedRoute,
31 private ngLocation: Location,
32 private renderer: Renderer2,
33 private auth: AuthService,
34 private org: OrgService,
35 private store: StoreService,
36 private offline: OfflineService
40 this.routeTo = this.route.snapshot.queryParamMap.get('routeTo');
43 if (this.routeTo.match(/^[a-z]+:\/\//i)) {
45 'routeTo must contain only path information: ', this.routeTo);
50 // clear out any stale auth data
54 this.renderer.selectRootElement('#username').focus();
56 this.store.getWorkstations()
58 this.workstations = wsList;
59 return this.store.getDefaultWorkstation();
61 this.args.workstation = def;
62 this.applyWorkstation();
67 const wanted = this.route.snapshot.queryParamMap.get('workstation');
68 if (!wanted) { return; } // use the default
70 const exists = this.workstations.filter(w => w.name === wanted)[0];
72 this.args.workstation = wanted;
74 console.error(`Unknown workstation requested: ${wanted}`);
81 let url: string = this.routeTo || SPLASH_PAGE_PATH;
83 // prevent sending the user back to the login page
84 if (url.match('/staff/login')) { url = SPLASH_PAGE_PATH; }
86 const workstation: string = this.args.workstation;
88 this.loginFailed = false;
89 this.auth.login(this.args).then(
92 if (this.auth.workstationState === AuthWsState.NOT_FOUND_SERVER) {
93 // User attempted to login with a workstation that is
94 // unknown to the server. Redirect to the WS admin page.
95 // Reset the WS state to avoid looping back to WS removal
96 // page before the new workstation can be activated.
97 this.auth.workstationState = AuthWsState.PENDING;
99 [`/staff/admin/workstation/workstations/remove/${workstation}`]);
103 this.offline.refreshOfflineData()
104 // Initial login clears cached org unit settings.
105 .then(_ => this.org.clearCachedSettings())
108 // Force reload of the app after a successful login.
109 // This allows the route resolver to re-run with a
110 // valid auth token and workstation.
111 window.location.href =
112 this.ngLocation.prepareExternalUrl(url);
117 this.loginFailed = true;