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';
8 templateUrl : './login.component.html'
11 export class StaffLoginComponent implements OnInit {
24 private router: Router,
25 private route: ActivatedRoute,
26 private ngLocation: Location,
27 private renderer: Renderer2,
28 private auth: AuthService,
29 private store: StoreService
33 // clear out any stale auth data
37 this.renderer.selectRootElement('#username').focus();
39 this.workstations = this.store.getLocalItem('eg.workstation.all');
40 this.args.workstation =
41 this.store.getLocalItem('eg.workstation.default');
42 this.applyWorkstation();
46 const wanted = this.route.snapshot.queryParamMap.get('workstation');
47 if (!wanted) { return; } // use the default
49 const exists = this.workstations.filter(w => w.name === wanted)[0];
51 this.args.workstation = wanted;
53 console.error(`Unknown workstation requested: ${wanted}`);
60 let url: string = this.auth.redirectUrl || '/staff/splash';
62 // prevent sending the user back to the login page
63 if (url.startsWith('/staff/login')) {
64 url = '/staff/splash';
67 const workstation: string = this.args.workstation;
69 this.loginFailed = false;
70 this.auth.login(this.args).then(
72 this.auth.redirectUrl = null;
74 if (this.auth.workstationState === AuthWsState.NOT_FOUND_SERVER) {
75 // User attempted to login with a workstation that is
76 // unknown to the server. Redirect to the WS admin page.
77 // Reset the WS state to avoid looping back to WS removal
78 // page before the new workstation can be activated.
79 this.auth.workstationState = AuthWsState.PENDING;
81 [`/staff/admin/workstation/workstations/remove/${workstation}`]);
83 // Force reload of the app after a successful login.
84 // This allows the route resolver to re-run with a
85 // valid auth token and workstation.
86 window.location.href =
87 this.ngLocation.prepareExternalUrl(url);
91 this.loginFailed = true;