2f4f67e711ad3eafca66573adff32602e91c76a3
[osm/NG-UI.git] / src / app / login / LoginComponent.ts
1 /*
2  Copyright 2020 TATA ELXSI
3
4  Licensed under the Apache License, Version 2.0 (the 'License');
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7
8     http://www.apache.org/licenses/LICENSE-2.0
9
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15
16  Author: KUMARAN M (kumaran.m@tataelxsi.co.in), RAJESH S (rajesh.s@tataelxsi.co.in), BARATH KUMAR R (barath.r@tataelxsi.co.in)
17 */
18
19 /**
20  * @file Page for Login component
21  */
22 import { HttpErrorResponse } from '@angular/common/http';
23 import { Component, Injector, OnInit } from '@angular/core';
24 import { FormBuilder, FormGroup, Validators } from '@angular/forms';
25 import { Router } from '@angular/router';
26 import { AuthenticationService } from 'AuthenticationService';
27 import { RestService } from 'RestService';
28 import { Observable } from 'rxjs';
29 import { SharedService } from 'SharedService';
30 import { isNullOrUndefined } from 'util';
31
32 /**
33  * Creating component
34  * @Component takes LoginComponent.html as template url
35  */
36 @Component({
37     selector: 'app-login',
38     templateUrl: './LoginComponent.html',
39     styleUrls: ['./LoginComponent.scss']
40 })
41 /** Exporting a class @exports LoginComponent */
42 export class LoginComponent implements OnInit {
43     /** Invoke service injectors @public */
44     public injector: Injector;
45
46     /** contains loginform group information @public */
47     public loginForm: FormGroup;
48
49     /** submitted set to boolean state @public */
50     public submitted: boolean = false;
51
52     /** contains return URL link @public */
53     public returnUrl: string;
54
55     /** Observable Hold the value of subscription  @public */
56     public isLoggedIn$: Observable<boolean>;
57
58     /** contains access token information @public */
59     public accessToken: string;
60
61     /** Utilizes rest service for any CRUD operations @public */
62     public restService: RestService;
63
64     /** Check the loading results @public */
65     public isLoadingResults: boolean = false;
66
67     /** Give the message for the loading @public */
68     public message: string = 'PLEASEWAIT';
69
70     /** Contains all methods related to shared @public */
71     public sharedService: SharedService;
72
73     /** Utilizes auth service for any auth operations @private */
74     private authService: AuthenticationService;
75
76     /** contians form builder module @private */
77     private formBuilder: FormBuilder;
78
79     /** Holds teh instance of AuthService class of type AuthService @private */
80     private router: Router;
81
82     // creates instance of login component
83     constructor(injector: Injector) {
84         this.injector = injector;
85         this.restService = this.injector.get(RestService);
86         this.authService = this.injector.get(AuthenticationService);
87         this.formBuilder = this.injector.get(FormBuilder);
88         this.router = this.injector.get(Router);
89         this.sharedService = this.injector.get(SharedService);
90     }
91
92     /**
93      * Lifecyle Hooks the trigger before component is instantiate
94      */
95     public ngOnInit(): void {
96         this.isLoggedIn$ = this.authService.isLoggedIn;
97         if (this.isLoggedIn$) {
98             this.router.navigate(['/']).catch(() => {
99                 // Catch Navigation Error
100             });
101         }
102         this.loginForm = this.formBuilder.group({
103             userName: ['', [Validators.required]],
104             password: ['', [Validators.required]]
105         });
106         this.returnUrl = isNullOrUndefined(localStorage.getItem('returnUrl')) ? '/' : localStorage.getItem('returnUrl');
107     }
108
109     /**
110      * called on form submit @private onSubmit
111      */
112     public onSubmit(): void {
113         this.submitted = true;
114         if (this.loginForm.invalid) {
115             return;
116         }
117         this.isLoadingResults = true;
118         this.sharedService.cleanForm(this.loginForm);
119         this.authService.login(this.loginForm.value.userName, this.loginForm.value.password).subscribe(
120             (data: {}) => {
121                 this.isLoadingResults = false;
122                 this.router.navigate([this.returnUrl]).catch(() => {
123                     // Catch Navigation Error
124                 });
125                 localStorage.removeItem('returnUrl');
126             }, (err: HttpErrorResponse) => {
127                 this.isLoadingResults = false;
128                 this.restService.handleError(err, 'post');
129             });
130     }
131 }