blob: 06d8688d3a9ff20d1105fe3fcfd126500c3d9f2c [file] [log] [blame]
kumaran.m3b4814a2020-05-01 19:48:54 +05301/*
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 */
22import { HttpErrorResponse } from '@angular/common/http';
23import { Component, Injector, OnInit } from '@angular/core';
24import { FormBuilder, FormGroup, Validators } from '@angular/forms';
25import { Router } from '@angular/router';
26import { AuthenticationService } from 'AuthenticationService';
27import { RestService } from 'RestService';
28import { Observable } from 'rxjs';
29import { SharedService } from 'SharedService';
30import { 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 */
42export 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.JSa9816552022-04-12 09:07:08 +053058 /** Observable Hold the value of subscription @public */
59 public isChangePassword$: Observable<boolean>;
60
kumaran.m3b4814a2020-05-01 19:48:54 +053061 /** 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.JSa9816552022-04-12 09:07:08 +053076 /** contains the loggedIn observable value @public */
77 public loggedIn: boolean;
78
79 /** contains the passwordIn observable value @public */
80 public changePassword: boolean;
81
kumaran.m3b4814a2020-05-01 19:48:54 +053082 /** 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.JSa9816552022-04-12 09:07:08 +0530106 this.isLoggedIn$.subscribe((res: boolean): void => {
107 this.loggedIn = res;
108 });
109 if (this.loggedIn === true) {
110 this.router.navigate(['/']).catch((): void => {
kumaran.m3b4814a2020-05-01 19:48:54 +0530111 // Catch Navigation Error
112 });
113 }
SANDHYA.JSa9816552022-04-12 09:07:08 +0530114 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.m3b4814a2020-05-01 19:48:54 +0530124 this.loginForm = this.formBuilder.group({
125 userName: ['', [Validators.required]],
126 password: ['', [Validators.required]]
127 });
SANDHYA.JS1f334d12023-05-10 22:58:14 +0530128 this.returnUrl = isNullOrUndefined(sessionStorage.getItem('returnUrl')) ? '/' : sessionStorage.getItem('returnUrl');
kumaran.m3b4814a2020-05-01 19:48:54 +0530129 }
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.JSa9816552022-04-12 09:07:08 +0530142 (data: {}): void => {
kumaran.m3b4814a2020-05-01 19:48:54 +0530143 this.isLoadingResults = false;
SANDHYA.JSa9816552022-04-12 09:07:08 +0530144 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 }
SANDHYA.JS1f334d12023-05-10 22:58:14 +0530153 sessionStorage.removeItem('returnUrl');
SANDHYA.JSa9816552022-04-12 09:07:08 +0530154 }, (err: HttpErrorResponse): void => {
kumaran.m3b4814a2020-05-01 19:48:54 +0530155 this.isLoadingResults = false;
156 this.restService.handleError(err, 'post');
157 });
158 }
159}