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 {
23 private router: Router,
24 private route: ActivatedRoute,
25 private ngLocation: Location,
26 private renderer: Renderer2,
27 private auth: AuthService,
28 private store: StoreService
32 // clear out any stale auth data
36 this.renderer.selectRootElement('#username').focus();
38 this.workstations = this.store.getLocalItem('eg.workstation.all');
39 this.args.workstation =
40 this.store.getLocalItem('eg.workstation.default');
41 this.applyWorkstation();
45 const wanted = this.route.snapshot.queryParamMap.get('workstation');
46 if (!wanted) { return; } // use the default
48 const exists = this.workstations.filter(w => w.name === wanted)[0];
50 this.args.workstation = wanted;
52 console.error(`Unknown workstation requested: ${wanted}`);
59 let url: string = this.auth.redirectUrl || '/staff/splash';
61 // prevent sending the user back to the login page
62 if (url.startsWith('/staff/login')) {
63 url = '/staff/splash';
66 const workstation: string = this.args.workstation;
68 this.auth.login(this.args).then(
70 this.auth.redirectUrl = null;
72 if (this.auth.workstationState === AuthWsState.NOT_FOUND_SERVER) {
73 // User attempted to login with a workstation that is
74 // unknown to the server. Redirect to the WS admin page.
75 // Reset the WS state to avoid looping back to WS removal
76 // page before the new workstation can be activated.
77 this.auth.workstationState = AuthWsState.PENDING;
79 [`/staff/admin/workstation/workstations/remove/${workstation}`]);
81 // Force reload of the app after a successful login.
82 // This allows the route resolver to re-run with a
83 // valid auth token and workstation.
84 window.location.href =
85 this.ngLocation.prepareExternalUrl(url);
89 // indicate failure in the UI.