]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/login.component.ts
2f2b9312e769b060fc5207e66980a9abaef039d9
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / login.component.ts
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';
6
7 @Component({
8   templateUrl : './login.component.html'
9 })
10
11 export class StaffLoginComponent implements OnInit {
12
13     workstations: any[];
14     loginFailed: boolean;
15
16     args = {
17       username : '',
18       password : '',
19       workstation : '',
20       type : 'staff'
21     };
22
23     constructor(
24       private router: Router,
25       private route: ActivatedRoute,
26       private ngLocation: Location,
27       private renderer: Renderer2,
28       private auth: AuthService,
29       private store: StoreService
30     ) {}
31
32     ngOnInit() {
33         // clear out any stale auth data
34         this.auth.logout();
35
36         // Focus username
37         this.renderer.selectRootElement('#username').focus();
38
39         this.store.getWorkstations()
40         .then(wsList => {
41             this.workstations = wsList;
42             return this.store.getDefaultWorkstation();
43         }).then(def => {
44             this.args.workstation = def;
45             this.applyWorkstation();
46         });
47     }
48
49     applyWorkstation() {
50         const wanted = this.route.snapshot.queryParamMap.get('workstation');
51         if (!wanted) { return; } // use the default
52
53         const exists = this.workstations.filter(w => w.name === wanted)[0];
54         if (exists) {
55             this.args.workstation = wanted;
56         } else {
57             console.error(`Unknown workstation requested: ${wanted}`);
58         }
59     }
60
61     handleSubmit() {
62
63         // post-login URL
64         let url: string = this.auth.redirectUrl || '/staff/splash';
65
66         // prevent sending the user back to the login page
67         if (url.startsWith('/staff/login')) {
68             url = '/staff/splash';
69         }
70
71         const workstation: string = this.args.workstation;
72
73         this.loginFailed = false;
74         this.auth.login(this.args).then(
75             ok => {
76                 this.auth.redirectUrl = null;
77
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;
84                     this.router.navigate(
85                         [`/staff/admin/workstation/workstations/remove/${workstation}`]);
86                 } else {
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);
92                 }
93             },
94             notOk => {
95                 this.loginFailed = true;
96             }
97         );
98     }
99 }
100
101
102