7 } from '@angular/core';
8 import {Location} from '@angular/common';
9 import {Router, ActivatedRoute} from '@angular/router';
10 import {AuthService, AuthWsState} from '@eg/core/auth.service';
11 import {StoreService} from '@eg/core/store.service';
12 import {OrgService} from '@eg/core/org.service';
13 import {OfflineService} from '@eg/staff/share/offline.service';
16 styleUrls: ['./login.component.css'],
17 templateUrl : './login.component.html'
19 export class StaffLoginComponent implements OnInit {
21 @ViewChild('password')
22 passwordInput: ElementRef;
26 pendingXactsDate: Date;
27 passwordVisible: boolean;
37 private router: Router,
38 private route: ActivatedRoute,
39 private ngLocation: Location,
40 private renderer: Renderer2,
41 private auth: AuthService,
42 private org: OrgService,
43 private store: StoreService,
44 private offline: OfflineService
48 this.routeTo = this.route.snapshot.queryParamMap.get('routeTo');
51 if (this.routeTo.match(/^[a-z]+:\/\//i)) {
53 'routeTo must contain only path information: ', this.routeTo);
58 // clear out any stale auth data
62 this.renderer.selectRootElement('#username').focus();
64 this.store.getWorkstations()
66 this.workstations = wsList;
67 return this.store.getDefaultWorkstation();
69 this.args.workstation = def;
70 this.applyWorkstation();
73 this.offline.pendingXactsDate().then(d => this.pendingXactsDate = d);
77 const wanted = this.route.snapshot.queryParamMap.get('workstation');
78 if (!wanted) { return; } // use the default
80 const exists = this.workstations.filter(w => w.name === wanted)[0];
82 this.args.workstation = wanted;
84 console.error(`Unknown workstation requested: ${wanted}`);
90 this.passwordVisible = false;
93 let url: string = this.routeTo || '/staff/splash';
95 // prevent sending the user back to the login page
96 if (url.match('/staff/login')) { url = '/staff/splash'; }
98 const workstation: string = this.args.workstation;
100 this.loginFailed = false;
101 this.auth.login(this.args).then(
104 if (this.auth.workstationState === AuthWsState.NOT_FOUND_SERVER) {
105 // User attempted to login with a workstation that is
106 // unknown to the server. Redirect to the WS admin page.
107 // Reset the WS state to avoid looping back to WS removal
108 // page before the new workstation can be activated.
109 this.auth.workstationState = AuthWsState.PENDING;
110 this.router.navigate(
111 [`/staff/admin/workstation/workstations/remove/${workstation}`]);
115 this.offline.refreshOfflineData()
116 // Initial login clears cached org unit settings.
117 .then(_ => this.org.clearCachedSettings())
120 // Force reload of the app after a successful login.
121 // This allows the route resolver to re-run with a
122 // valid auth token and workstation.
123 window.location.href =
124 this.ngLocation.prepareExternalUrl(url);
129 this.loginFailed = true;
134 togglePasswordVisibility() {
135 this.passwordVisible = !this.passwordVisible;
136 this.passwordInput.nativeElement.focus();