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 {
17 pendingXactsDate: Date;
27 private router: Router,
28 private route: ActivatedRoute,
29 private ngLocation: Location,
30 private renderer: Renderer2,
31 private auth: AuthService,
32 private org: OrgService,
33 private store: StoreService,
34 private offline: OfflineService
38 this.routeTo = this.route.snapshot.queryParamMap.get('routeTo');
41 if (this.routeTo.match(/^[a-z]+:\/\//i)) {
43 'routeTo must contain only path information: ', this.routeTo);
48 // clear out any stale auth data
52 this.renderer.selectRootElement('#username').focus();
54 this.store.getWorkstations()
56 this.workstations = wsList;
57 return this.store.getDefaultWorkstation();
59 this.args.workstation = def;
60 this.applyWorkstation();
63 this.offline.pendingXactsDate().then(d => this.pendingXactsDate = d);
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 || '/staff/splash';
83 // prevent sending the user back to the login page
84 if (url.match('/staff/login')) { url = '/staff/splash'; }
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;