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;
28 ariaDescription: string = $localize`Your password is not visible.`;
38 private router: Router,
39 private route: ActivatedRoute,
40 private ngLocation: Location,
41 private renderer: Renderer2,
42 private auth: AuthService,
43 private org: OrgService,
44 private store: StoreService,
45 private offline: OfflineService
49 this.routeTo = this.route.snapshot.queryParamMap.get('routeTo');
52 if (this.routeTo.match(/^[a-z]+:\/\//i)) {
54 'routeTo must contain only path information: ', this.routeTo);
59 // clear out any stale auth data
63 this.renderer.selectRootElement('#username').focus();
65 this.store.getWorkstations()
67 this.workstations = wsList;
68 return this.store.getDefaultWorkstation();
70 this.args.workstation = def;
71 this.applyWorkstation();
74 this.offline.pendingXactsDate().then(d => this.pendingXactsDate = d);
78 const wanted = this.route.snapshot.queryParamMap.get('workstation');
79 if (!wanted) { return; } // use the default
81 const exists = this.workstations.filter(w => w.name === wanted)[0];
83 this.args.workstation = wanted;
85 console.error(`Unknown workstation requested: ${wanted}`);
91 this.passwordVisible = false;
94 let url: string = this.routeTo || '/staff/splash';
96 // prevent sending the user back to the login page
97 if (url.match('/staff/login')) { url = '/staff/splash'; }
99 const workstation: string = this.args.workstation;
101 this.loginFailed = false;
102 this.auth.login(this.args).then(
105 if (this.auth.workstationState === AuthWsState.NOT_FOUND_SERVER) {
106 // User attempted to login with a workstation that is
107 // unknown to the server. Redirect to the WS admin page.
108 // Reset the WS state to avoid looping back to WS removal
109 // page before the new workstation can be activated.
110 this.auth.workstationState = AuthWsState.PENDING;
111 this.router.navigate(
112 [`/staff/admin/workstation/workstations/remove/${workstation}`]);
116 this.offline.refreshOfflineData()
117 // Initial login clears cached org unit settings.
118 .then(_ => this.org.clearCachedSettings())
121 // Force reload of the app after a successful login.
122 // This allows the route resolver to re-run with a
123 // valid auth token and workstation.
124 window.location.href =
125 this.ngLocation.prepareExternalUrl(url);
130 this.loginFailed = true;
135 togglePasswordVisibility() {
136 this.passwordVisible = !this.passwordVisible;
137 // eslint-disable-next-line max-len
138 if(this.passwordVisible) {this.ariaDescription = $localize`Your password is visible!`;} else {this.ariaDescription = $localize`Your password is not visible.`;}
139 this.passwordInput.nativeElement.focus();