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.store.getWorkstations()
41 this.workstations = wsList;
42 return this.store.getDefaultWorkstation();
44 this.args.workstation = def;
45 this.applyWorkstation();
50 const wanted = this.route.snapshot.queryParamMap.get('workstation');
51 if (!wanted) { return; } // use the default
53 const exists = this.workstations.filter(w => w.name === wanted)[0];
55 this.args.workstation = wanted;
57 console.error(`Unknown workstation requested: ${wanted}`);
64 let url: string = this.auth.redirectUrl || '/staff/splash';
66 // prevent sending the user back to the login page
67 if (url.startsWith('/staff/login')) {
68 url = '/staff/splash';
71 const workstation: string = this.args.workstation;
73 this.loginFailed = false;
74 this.auth.login(this.args).then(
76 this.auth.redirectUrl = null;
78 if (this.auth.workstationState === AuthWsState.NOT_FOUND_SERVER) {
79 // User attempted to login with a workstation that is
80 // unknown to the server. Redirect to the WS admin page.
81 // Reset the WS state to avoid looping back to WS removal
82 // page before the new workstation can be activated.
83 this.auth.workstationState = AuthWsState.PENDING;
85 [`/staff/admin/workstation/workstations/remove/${workstation}`]);
87 // Force reload of the app after a successful login.
88 // This allows the route resolver to re-run with a
89 // valid auth token and workstation.
90 window.location.href =
91 this.ngLocation.prepareExternalUrl(url);
95 this.loginFailed = true;