2 Copyright 2020 TATA ELXSI
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
16 Author: KUMARAN M (kumaran.m@tataelxsi.co.in), RAJESH S (rajesh.s@tataelxsi.co.in), BARATH KUMAR R (barath.r@tataelxsi.co.in)
20 * @file Page for Login component
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';
34 * @Component takes LoginComponent.html as template url
37 selector: 'app-login',
38 templateUrl: './LoginComponent.html',
39 styleUrls: ['./LoginComponent.scss']
41 /** Exporting a class @exports LoginComponent */
42 export class LoginComponent implements OnInit {
43 /** Invoke service injectors @public */
44 public injector: Injector;
46 /** contains loginform group information @public */
47 public loginForm: FormGroup;
49 /** submitted set to boolean state @public */
50 public submitted: boolean = false;
52 /** contains return URL link @public */
53 public returnUrl: string;
55 /** Observable Hold the value of subscription @public */
56 public isLoggedIn$: Observable<boolean>;
58 /** Observable Hold the value of subscription @public */
59 public isChangePassword$: Observable<boolean>;
61 /** contains access token information @public */
62 public accessToken: string;
64 /** Utilizes rest service for any CRUD operations @public */
65 public restService: RestService;
67 /** Check the loading results @public */
68 public isLoadingResults: boolean = false;
70 /** Give the message for the loading @public */
71 public message: string = 'PLEASEWAIT';
73 /** Contains all methods related to shared @public */
74 public sharedService: SharedService;
76 /** contains the loggedIn observable value @public */
77 public loggedIn: boolean;
79 /** contains the passwordIn observable value @public */
80 public changePassword: boolean;
82 /** Utilizes auth service for any auth operations @private */
83 private authService: AuthenticationService;
85 /** contians form builder module @private */
86 private formBuilder: FormBuilder;
88 /** Holds teh instance of AuthService class of type AuthService @private */
89 private router: Router;
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);
102 * Lifecyle Hooks the trigger before component is instantiate
104 public ngOnInit(): void {
105 this.isLoggedIn$ = this.authService.isLoggedIn;
106 this.isLoggedIn$.subscribe((res: boolean): void => {
109 if (this.loggedIn === true) {
110 this.router.navigate(['/']).catch((): void => {
111 // Catch Navigation Error
114 this.isChangePassword$ = this.authService.isChangePassword;
115 this.isChangePassword$.subscribe((res: boolean): void => {
116 this.changePassword = res;
118 if (this.changePassword === true) {
119 this.router.navigate(['changepassword']).catch((): void => {
120 // Catch Navigation Error
124 this.loginForm = this.formBuilder.group({
125 userName: ['', [Validators.required]],
126 password: ['', [Validators.required]]
128 this.returnUrl = isNullOrUndefined(localStorage.getItem('returnUrl')) ? '/' : localStorage.getItem('returnUrl');
132 * called on form submit @private onSubmit
134 public onSubmit(): void {
135 this.submitted = true;
136 if (this.loginForm.invalid) {
139 this.isLoadingResults = true;
140 this.sharedService.cleanForm(this.loginForm);
141 this.authService.login(this.loginForm.value.userName, this.loginForm.value.password).subscribe(
142 (data: {}): void => {
143 this.isLoadingResults = false;
144 if (this.changePassword === true && this.loggedIn === false) {
145 this.router.navigate(['/changepassword']).catch((): void => {
146 // Catch Navigation Error
149 this.router.navigate([this.returnUrl]).catch((): void => {
150 // Catch Navigation Error
153 localStorage.removeItem('returnUrl');
154 }, (err: HttpErrorResponse): void => {
155 this.isLoadingResults = false;
156 this.restService.handleError(err, 'post');