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;
18 passwordVisible: boolean;
28 private router: Router,
29 private route: ActivatedRoute,
30 private ngLocation: Location,
31 private renderer: Renderer2,
32 private auth: AuthService,
33 private org: OrgService,
34 private store: StoreService,
35 private offline: OfflineService
39 this.routeTo = this.route.snapshot.queryParamMap.get('routeTo');
42 if (this.routeTo.match(/^[a-z]+:\/\//i)) {
44 'routeTo must contain only path information: ', this.routeTo);
49 // clear out any stale auth data
53 this.renderer.selectRootElement('#username').focus();
55 this.store.getWorkstations()
57 this.workstations = wsList;
58 return this.store.getDefaultWorkstation();
60 this.args.workstation = def;
61 this.applyWorkstation();
64 this.offline.pendingXactsDate().then(d => this.pendingXactsDate = d);
68 const wanted = this.route.snapshot.queryParamMap.get('workstation');
69 if (!wanted) { return; } // use the default
71 const exists = this.workstations.filter(w => w.name === wanted)[0];
73 this.args.workstation = wanted;
75 console.error(`Unknown workstation requested: ${wanted}`);
81 this.passwordVisible = false;
84 let url: string = this.routeTo || '/staff/splash';
86 // prevent sending the user back to the login page
87 if (url.match('/staff/login')) { url = '/staff/splash'; }
89 const workstation: string = this.args.workstation;
91 this.loginFailed = false;
92 this.auth.login(this.args).then(
95 if (this.auth.workstationState === AuthWsState.NOT_FOUND_SERVER) {
96 // User attempted to login with a workstation that is
97 // unknown to the server. Redirect to the WS admin page.
98 // Reset the WS state to avoid looping back to WS removal
99 // page before the new workstation can be activated.
100 this.auth.workstationState = AuthWsState.PENDING;
101 this.router.navigate(
102 [`/staff/admin/workstation/workstations/remove/${workstation}`]);
106 this.offline.refreshOfflineData()
107 // Initial login clears cached org unit settings.
108 .then(_ => this.org.clearCachedSettings())
111 // Force reload of the app after a successful login.
112 // This allows the route resolver to re-run with a
113 // valid auth token and workstation.
114 window.location.href =
115 this.ngLocation.prepareExternalUrl(url);
120 this.loginFailed = true;
125 togglePasswordVisibility() {
126 this.passwordVisible = !this.passwordVisible;