| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 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 | */ |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame^] | 22 | import { isNullOrUndefined } from 'util'; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 23 | import { HttpErrorResponse } from '@angular/common/http'; |
| 24 | import { Component, Injector, OnInit } from '@angular/core'; |
| 25 | import { FormBuilder, FormGroup, Validators } from '@angular/forms'; |
| 26 | import { Router } from '@angular/router'; |
| 27 | import { AuthenticationService } from 'AuthenticationService'; |
| 28 | import { RestService } from 'RestService'; |
| 29 | import { Observable } from 'rxjs'; |
| 30 | import { SharedService } from 'SharedService'; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 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 | |
| SANDHYA.JS | a981655 | 2022-04-12 09:07:08 +0530 | [diff] [blame] | 58 | /** Observable Hold the value of subscription @public */ |
| 59 | public isChangePassword$: Observable<boolean>; |
| 60 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 61 | /** contains access token information @public */ |
| 62 | public accessToken: string; |
| 63 | |
| 64 | /** Utilizes rest service for any CRUD operations @public */ |
| 65 | public restService: RestService; |
| 66 | |
| 67 | /** Check the loading results @public */ |
| 68 | public isLoadingResults: boolean = false; |
| 69 | |
| 70 | /** Give the message for the loading @public */ |
| 71 | public message: string = 'PLEASEWAIT'; |
| 72 | |
| 73 | /** Contains all methods related to shared @public */ |
| 74 | public sharedService: SharedService; |
| 75 | |
| SANDHYA.JS | a981655 | 2022-04-12 09:07:08 +0530 | [diff] [blame] | 76 | /** contains the loggedIn observable value @public */ |
| 77 | public loggedIn: boolean; |
| 78 | |
| 79 | /** contains the passwordIn observable value @public */ |
| 80 | public changePassword: boolean; |
| 81 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 82 | /** Utilizes auth service for any auth operations @private */ |
| 83 | private authService: AuthenticationService; |
| 84 | |
| 85 | /** contians form builder module @private */ |
| 86 | private formBuilder: FormBuilder; |
| 87 | |
| 88 | /** Holds teh instance of AuthService class of type AuthService @private */ |
| 89 | private router: Router; |
| 90 | |
| 91 | // creates instance of login component |
| 92 | constructor(injector: Injector) { |
| 93 | this.injector = injector; |
| 94 | this.restService = this.injector.get(RestService); |
| 95 | this.authService = this.injector.get(AuthenticationService); |
| 96 | this.formBuilder = this.injector.get(FormBuilder); |
| 97 | this.router = this.injector.get(Router); |
| 98 | this.sharedService = this.injector.get(SharedService); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Lifecyle Hooks the trigger before component is instantiate |
| 103 | */ |
| 104 | public ngOnInit(): void { |
| 105 | this.isLoggedIn$ = this.authService.isLoggedIn; |
| SANDHYA.JS | a981655 | 2022-04-12 09:07:08 +0530 | [diff] [blame] | 106 | this.isLoggedIn$.subscribe((res: boolean): void => { |
| 107 | this.loggedIn = res; |
| 108 | }); |
| 109 | if (this.loggedIn === true) { |
| 110 | this.router.navigate(['/']).catch((): void => { |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 111 | // Catch Navigation Error |
| 112 | }); |
| 113 | } |
| SANDHYA.JS | a981655 | 2022-04-12 09:07:08 +0530 | [diff] [blame] | 114 | this.isChangePassword$ = this.authService.isChangePassword; |
| 115 | this.isChangePassword$.subscribe((res: boolean): void => { |
| 116 | this.changePassword = res; |
| 117 | }); |
| 118 | if (this.changePassword === true) { |
| 119 | this.router.navigate(['changepassword']).catch((): void => { |
| 120 | // Catch Navigation Error |
| 121 | }); |
| 122 | } |
| 123 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 124 | this.loginForm = this.formBuilder.group({ |
| 125 | userName: ['', [Validators.required]], |
| 126 | password: ['', [Validators.required]] |
| 127 | }); |
| 128 | this.returnUrl = isNullOrUndefined(localStorage.getItem('returnUrl')) ? '/' : localStorage.getItem('returnUrl'); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * called on form submit @private onSubmit |
| 133 | */ |
| 134 | public onSubmit(): void { |
| 135 | this.submitted = true; |
| 136 | if (this.loginForm.invalid) { |
| 137 | return; |
| 138 | } |
| 139 | this.isLoadingResults = true; |
| 140 | this.sharedService.cleanForm(this.loginForm); |
| 141 | this.authService.login(this.loginForm.value.userName, this.loginForm.value.password).subscribe( |
| SANDHYA.JS | a981655 | 2022-04-12 09:07:08 +0530 | [diff] [blame] | 142 | (data: {}): void => { |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 143 | this.isLoadingResults = false; |
| SANDHYA.JS | a981655 | 2022-04-12 09:07:08 +0530 | [diff] [blame] | 144 | if (this.changePassword === true && this.loggedIn === false) { |
| 145 | this.router.navigate(['/changepassword']).catch((): void => { |
| 146 | // Catch Navigation Error |
| 147 | }); |
| 148 | } else { |
| 149 | this.router.navigate([this.returnUrl]).catch((): void => { |
| 150 | // Catch Navigation Error |
| 151 | }); |
| 152 | } |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 153 | localStorage.removeItem('returnUrl'); |
| SANDHYA.JS | a981655 | 2022-04-12 09:07:08 +0530 | [diff] [blame] | 154 | }, (err: HttpErrorResponse): void => { |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 155 | this.isLoadingResults = false; |
| 156 | this.restService.handleError(err, 'post'); |
| 157 | }); |
| 158 | } |
| 159 | } |