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';
10 templateUrl : './login.component.html'
12 export class StaffLoginComponent implements OnInit {
26 private router: Router,
27 private route: ActivatedRoute,
28 private ngLocation: Location,
29 private renderer: Renderer2,
30 private auth: AuthService,
31 private org: OrgService,
32 private store: StoreService,
33 private offline: OfflineService
37 this.routeTo = this.route.snapshot.queryParamMap.get('routeTo');
40 if (this.routeTo.match(/^[a-z]+:\/\//i)) {
42 'routeTo must contain only path information: ', this.routeTo);
47 // clear out any stale auth data
51 this.renderer.selectRootElement('#username').focus();
53 this.store.getWorkstations()
55 this.workstations = wsList;
56 return this.store.getDefaultWorkstation();
58 this.args.workstation = def;
59 this.applyWorkstation();
64 const wanted = this.route.snapshot.queryParamMap.get('workstation');
65 if (!wanted) { return; } // use the default
67 const exists = this.workstations.filter(w => w.name === wanted)[0];
69 this.args.workstation = wanted;
71 console.error(`Unknown workstation requested: ${wanted}`);
78 let url: string = this.routeTo || '/staff/splash';
80 // prevent sending the user back to the login page
81 if (url.match('/staff/login')) { url = '/staff/splash'; }
83 const workstation: string = this.args.workstation;
85 this.loginFailed = false;
86 this.auth.login(this.args).then(
89 if (this.auth.workstationState === AuthWsState.NOT_FOUND_SERVER) {
90 // User attempted to login with a workstation that is
91 // unknown to the server. Redirect to the WS admin page.
92 // Reset the WS state to avoid looping back to WS removal
93 // page before the new workstation can be activated.
94 this.auth.workstationState = AuthWsState.PENDING;
96 [`/staff/admin/workstation/workstations/remove/${workstation}`]);
100 this.offline.refreshOfflineData()
101 // Initial login clears cached org unit settings.
102 .then(_ => this.org.clearCachedSettings())
105 // Force reload of the app after a successful login.
106 // This allows the route resolver to re-run with a
107 // valid auth token and workstation.
108 window.location.href =
109 this.ngLocation.prepareExternalUrl(url);
114 this.loginFailed = true;