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 /** contains access token information @public */
59 public accessToken: string;
61 /** Utilizes rest service for any CRUD operations @public */
62 public restService: RestService;
64 /** Check the loading results @public */
65 public isLoadingResults: boolean = false;
67 /** Give the message for the loading @public */
68 public message: string = 'PLEASEWAIT';
70 /** Contains all methods related to shared @public */
71 public sharedService: SharedService;
73 /** Utilizes auth service for any auth operations @private */
74 private authService: AuthenticationService;
76 /** contians form builder module @private */
77 private formBuilder: FormBuilder;
79 /** Holds teh instance of AuthService class of type AuthService @private */
80 private router: Router;
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);
93 * Lifecyle Hooks the trigger before component is instantiate
95 public ngOnInit(): void {
96 this.isLoggedIn$ = this.authService.isLoggedIn;
97 if (this.isLoggedIn$) {
98 this.router.navigate(['/']).catch(() => {
99 // Catch Navigation Error
102 this.loginForm = this.formBuilder.group({
103 userName: ['', [Validators.required]],
104 password: ['', [Validators.required]]
106 this.returnUrl = isNullOrUndefined(localStorage.getItem('returnUrl')) ? '/' : localStorage.getItem('returnUrl');
110 * called on form submit @private onSubmit
112 public onSubmit(): void {
113 this.submitted = true;
114 if (this.loginForm.invalid) {
117 this.isLoadingResults = true;
118 this.sharedService.cleanForm(this.loginForm);
119 this.authService.login(this.loginForm.value.userName, this.loginForm.value.password).subscribe(
121 this.isLoadingResults = false;
122 this.router.navigate([this.returnUrl]).catch(() => {
123 // Catch Navigation Error
125 localStorage.removeItem('returnUrl');
126 }, (err: HttpErrorResponse) => {
127 this.isLoadingResults = false;
128 this.restService.handleError(err, 'post');