]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/login.component.ts
LP1823041 Angular dialogs return observables
[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
15     args = {
16       username : '',
17       password : '',
18       workstation : '',
19       type : 'staff'
20     };
21
22     constructor(
23       private router: Router,
24       private route: ActivatedRoute,
25       private ngLocation: Location,
26       private renderer: Renderer2,
27       private auth: AuthService,
28       private store: StoreService
29     ) {}
30
31     ngOnInit() {
32         // clear out any stale auth data
33         this.auth.logout();
34
35         // Focus username
36         this.renderer.selectRootElement('#username').focus();
37
38         this.workstations = this.store.getLocalItem('eg.workstation.all');
39         this.args.workstation =
40             this.store.getLocalItem('eg.workstation.default');
41         this.applyWorkstation();
42     }
43
44     applyWorkstation() {
45         const wanted = this.route.snapshot.queryParamMap.get('workstation');
46         if (!wanted) { return; } // use the default
47
48         const exists = this.workstations.filter(w => w.name === wanted)[0];
49         if (exists) {
50             this.args.workstation = wanted;
51         } else {
52             console.error(`Unknown workstation requested: ${wanted}`);
53         }
54     }
55
56     handleSubmit() {
57
58         // post-login URL
59         let url: string = this.auth.redirectUrl || '/staff/splash';
60
61         // prevent sending the user back to the login page
62         if (url.startsWith('/staff/login')) {
63             url = '/staff/splash';
64         }
65
66         const workstation: string = this.args.workstation;
67
68         this.auth.login(this.args).then(
69             ok => {
70                 this.auth.redirectUrl = null;
71
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;
78                     this.router.navigate(
79                         [`/staff/admin/workstation/workstations/remove/${workstation}`]);
80                 } else {
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);
86                 }
87             },
88             notOk => {
89                 // indicate failure in the UI.
90             }
91         );
92     }
93 }
94
95
96