]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/login.component.ts
LP 2061136 follow-up: ng lint --fix
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / login.component.ts
1 import {
2     Component,
3     ElementRef,
4     OnInit,
5     Renderer2,
6     ViewChild
7 } from '@angular/core';
8 import {Location} from '@angular/common';
9 import {Router, ActivatedRoute} from '@angular/router';
10 import {AuthService, AuthWsState} from '@eg/core/auth.service';
11 import {StoreService} from '@eg/core/store.service';
12 import {OrgService} from '@eg/core/org.service';
13 import {OfflineService} from '@eg/staff/share/offline.service';
14
15 @Component({
16     styleUrls: ['./login.component.css'],
17     templateUrl : './login.component.html'
18 })
19 export class StaffLoginComponent implements OnInit {
20
21     @ViewChild('password')
22         passwordInput: ElementRef;
23     workstations: any[];
24     loginFailed: boolean;
25     routeTo: string;
26     pendingXactsDate: Date;
27     passwordVisible: boolean;
28
29     args = {
30         username : '',
31         password : '',
32         workstation : '',
33         type : 'staff'
34     };
35
36     constructor(
37       private router: Router,
38       private route: ActivatedRoute,
39       private ngLocation: Location,
40       private renderer: Renderer2,
41       private auth: AuthService,
42       private org: OrgService,
43       private store: StoreService,
44       private offline: OfflineService
45     ) {}
46
47     ngOnInit() {
48         this.routeTo = this.route.snapshot.queryParamMap.get('routeTo');
49
50         if (this.routeTo) {
51             if (this.routeTo.match(/^[a-z]+:\/\//i)) {
52                 console.warn(
53                     'routeTo must contain only path information: ', this.routeTo);
54                 this.routeTo = null;
55             }
56         }
57
58         // clear out any stale auth data
59         this.auth.logout();
60
61         // Focus username
62         this.renderer.selectRootElement('#username').focus();
63
64         this.store.getWorkstations()
65             .then(wsList => {
66                 this.workstations = wsList;
67                 return this.store.getDefaultWorkstation();
68             }).then(def => {
69                 this.args.workstation = def;
70                 this.applyWorkstation();
71             });
72
73         this.offline.pendingXactsDate().then(d => this.pendingXactsDate = d);
74     }
75
76     applyWorkstation() {
77         const wanted = this.route.snapshot.queryParamMap.get('workstation');
78         if (!wanted) { return; } // use the default
79
80         const exists = this.workstations.filter(w => w.name === wanted)[0];
81         if (exists) {
82             this.args.workstation = wanted;
83         } else {
84             console.error(`Unknown workstation requested: ${wanted}`);
85         }
86     }
87
88     handleSubmit() {
89
90         this.passwordVisible = false;
91
92         // post-login URL
93         let url: string = this.routeTo || '/staff/splash';
94
95         // prevent sending the user back to the login page
96         if (url.match('/staff/login')) { url = '/staff/splash'; }
97
98         const workstation: string = this.args.workstation;
99
100         this.loginFailed = false;
101         this.auth.login(this.args).then(
102             ok => {
103
104                 if (this.auth.workstationState === AuthWsState.NOT_FOUND_SERVER) {
105                     // User attempted to login with a workstation that is
106                     // unknown to the server. Redirect to the WS admin page.
107                     // Reset the WS state to avoid looping back to WS removal
108                     // page before the new workstation can be activated.
109                     this.auth.workstationState = AuthWsState.PENDING;
110                     this.router.navigate(
111                         [`/staff/admin/workstation/workstations/remove/${workstation}`]);
112
113                 } else {
114
115                     this.offline.refreshOfflineData()
116                     // Initial login clears cached org unit settings.
117                         .then(_ => this.org.clearCachedSettings())
118                         .then(_ => {
119
120                             // Force reload of the app after a successful login.
121                             // This allows the route resolver to re-run with a
122                             // valid auth token and workstation.
123                             window.location.href =
124                             this.ngLocation.prepareExternalUrl(url);
125                         });
126                 }
127             },
128             notOk => {
129                 this.loginFailed = true;
130             }
131         );
132     }
133
134     togglePasswordVisibility() {
135         this.passwordVisible = !this.passwordVisible;
136         this.passwordInput.nativeElement.focus();
137     }
138
139 }
140
141
142