X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=src%2Fservices%2FAuthenticationService.ts;h=5eda6f9c0d45ee2d8fa302a427b7eb1beda23e61;hb=0a34dfa32165036b380ec6ac493469b34007df0a;hp=728d4ac51ea4fa3cd1017ab096e85acfbe1272dc;hpb=3b4814aa2d3dec621dadb52f058ba95a3dc3a86a;p=osm%2FNG-UI.git diff --git a/src/services/AuthenticationService.ts b/src/services/AuthenticationService.ts index 728d4ac..5eda6f9 100644 --- a/src/services/AuthenticationService.ts +++ b/src/services/AuthenticationService.ts @@ -18,6 +18,7 @@ /** * @file Auth service */ +import { isNullOrUndefined } from 'util'; import { HttpHeaders } from '@angular/common/http'; import { Injectable, Injector } from '@angular/core'; import { Router } from '@angular/router'; @@ -27,7 +28,6 @@ import { APIURLHEADER, ERRORDATA } from 'CommonModel'; import { environment } from 'environment'; import { BehaviorSubject, Observable } from 'rxjs'; import { map } from 'rxjs/operators'; -import { isNullOrUndefined } from 'util'; import { ProjectModel } from '../models/VNFDModel'; import { RestService } from './RestService'; @@ -37,24 +37,6 @@ import { RestService } from './RestService'; */ @Injectable() export class AuthenticationService { - /** - * Get method for Observable loggedIn - */ - get isLoggedIn(): Observable { - return this.loggedIn.asObservable(); - } - - /** - * Get method for Observable Username - */ - get username(): Observable { - return this.userName.asObservable(); - } - - /** Get method for project name */ - get ProjectName(): Observable { - return this.projectName$.asObservable(); - } /** To inject services @public */ public injector: Injector; @@ -79,6 +61,9 @@ export class AuthenticationService { /** Holds the logged in condition of type BehaviorSubject @private */ private loggedIn: BehaviorSubject = new BehaviorSubject(false); + /** Holds the change password in condition of type BehaviorSubject @private */ + private changePassword: BehaviorSubject = new BehaviorSubject(false); + /** Hold Rest Service Objects */ private restService: RestService; @@ -98,12 +83,43 @@ export class AuthenticationService { this.restService = this.injector.get(RestService); this.modalService = this.injector.get(NgbModal); this.idle = this.injector.get(Idle); - if (localStorage.getItem('id_token') !== null) { + if (localStorage.getItem('username') !== null) { this.loggedIn.next(true); + this.changePassword.next(false); + } else if (localStorage.getItem('firstLogin') !== null) { + this.changePassword.next(true); + this.loggedIn.next(false); } else { this.loggedIn.next(false); } this.userName.next(localStorage.getItem('username')); + this.redirectToPage(); + } + + /** + * Get method for Observable loggedIn + */ + get isLoggedIn(): Observable { + return this.loggedIn.asObservable(); + } + + /** + * Get method for Observable changepassword + */ + get isChangePassword(): Observable { + return this.changePassword.asObservable(); + } + + /** + * Get method for Observable Username + */ + get username(): Observable { + return this.userName.asObservable(); + } + + /** Get method for project name */ + get ProjectName(): Observable { + return this.projectName$.asObservable(); } /** @@ -118,8 +134,16 @@ export class AuthenticationService { httpOptions: { headers: this.httpOptions } }; return this.restService.postResource(apiURLHeader, this.payLoad) - .pipe(map((data: ProjectModel) => { - if (data) { + .pipe(map((data: ProjectModel): BehaviorSubject => { + if (data.message === 'change_password') { + localStorage.setItem('firstLogin', 'true'); + localStorage.setItem('id_token', data.id); + localStorage.setItem('user_id', data.user_id); + this.idle.watch(true); + this.changePassword.next(true); + this.loggedIn.next(false); + return this.changePassword; + } else { this.setLocalStorage(data); this.idle.watch(true); this.loggedIn.next(true); @@ -127,7 +151,7 @@ export class AuthenticationService { this.userName.next(data.username); return this.loggedIn; } - }, (error: ERRORDATA) => { this.restService.handleError(error, 'post'); } + }, (error: ERRORDATA): void => { this.restService.handleError(error, 'post'); } )); } @@ -162,14 +186,19 @@ export class AuthenticationService { /** Destory tokens API response handling @public */ public logoutResponse(): void { this.loggedIn.next(false); + this.changePassword.next(false); const langCode: string = localStorage.getItem('languageCode'); const redirecturl: string = isNullOrUndefined(localStorage.getItem('returnUrl')) ? '/' : localStorage.getItem('returnUrl'); + const osmVersion: string = isNullOrUndefined(localStorage.getItem('osmVersion')) ? '' : localStorage.getItem('osmVersion'); localStorage.clear(); localStorage.setItem('languageCode', langCode); localStorage.setItem('returnUrl', redirecturl); localStorage.setItem('token_state', null); + localStorage.setItem('osmVersion', osmVersion); this.idle.stop(); - this.router.navigate(['login']).catch(); + this.router.navigate(['login']).catch((): void => { + // Catch Navigation Error + }); } /** * Logout the user & clearing the token. @@ -180,16 +209,27 @@ export class AuthenticationService { this.modalService.dismissAll(); this.destoryToken(); } - /** Destory tokens on logout @private */ - private destoryToken(): void { + /** Destory tokens on logout @public */ + public destoryToken(): void { const tokenID: string = localStorage.getItem('id_token'); if (tokenID !== null) { const deletingURl: string = environment.GENERATETOKEN_URL + '/' + tokenID; - this.restService.deleteResource(deletingURl).subscribe((res: {}) => { + this.restService.deleteResource(deletingURl).subscribe((res: {}): void => { this.logoutResponse(); - }, (error: ERRORDATA) => { + }, (error: ERRORDATA): void => { this.restService.handleError(error, 'delete'); }); } } + + /** Return to previous page deny access to changepassword */ + public redirectToPage(): void { + if (window.location.pathname === '/changepassword' && localStorage.getItem('username') !== null) { + window.history.back(); + } else if (window.location.pathname === '/' && localStorage.getItem('firstLogin') === 'true') { + this.router.navigate(['/login']).catch((): void => { + // Catch Navigation Error + }); + } + } }