X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=src%2Fservices%2FAuthenticationService.ts;h=d01eec6aa510786ae11fe2ad0477779d97c08dbb;hb=refs%2Fheads%2Fmaster;hp=5eda6f9c0d45ee2d8fa302a427b7eb1beda23e61;hpb=0a34dfa32165036b380ec6ac493469b34007df0a;p=osm%2FNG-UI.git diff --git a/src/services/AuthenticationService.ts b/src/services/AuthenticationService.ts index 5eda6f9..9b0f525 100644 --- a/src/services/AuthenticationService.ts +++ b/src/services/AuthenticationService.ts @@ -18,7 +18,6 @@ /** * @file Auth service */ -import { isNullOrUndefined } from 'util'; import { HttpHeaders } from '@angular/common/http'; import { Injectable, Injector } from '@angular/core'; import { Router } from '@angular/router'; @@ -28,6 +27,8 @@ import { APIURLHEADER, ERRORDATA } from 'CommonModel'; import { environment } from 'environment'; import { BehaviorSubject, Observable } from 'rxjs'; import { map } from 'rxjs/operators'; +import { isNullOrUndefined } from 'SharedService'; +import { SharedService } from 'SharedService'; import { ProjectModel } from '../models/VNFDModel'; import { RestService } from './RestService'; @@ -64,6 +65,9 @@ export class AuthenticationService { /** Holds the change password in condition of type BehaviorSubject @private */ private changePassword: BehaviorSubject = new BehaviorSubject(false); + /** Holds the forgotpassword in condition of type BehaviorSubject @public */ + public forgotPassword: BehaviorSubject = new BehaviorSubject(false); + /** Hold Rest Service Objects */ private restService: RestService; @@ -76,6 +80,9 @@ export class AuthenticationService { /** handle idle time out service @private */ private idle: Idle; + /** Contains all methods related to shared @private */ + private sharedService: SharedService; + /** create the instance of the component */ constructor(injector: Injector) { this.injector = injector; @@ -83,16 +90,17 @@ export class AuthenticationService { this.restService = this.injector.get(RestService); this.modalService = this.injector.get(NgbModal); this.idle = this.injector.get(Idle); - if (localStorage.getItem('username') !== null) { + this.sharedService = this.injector.get(SharedService); + if (sessionStorage.getItem('username') !== null) { this.loggedIn.next(true); this.changePassword.next(false); - } else if (localStorage.getItem('firstLogin') !== null) { + } else if (sessionStorage.getItem('firstLogin') !== null) { this.changePassword.next(true); this.loggedIn.next(false); } else { this.loggedIn.next(false); } - this.userName.next(localStorage.getItem('username')); + this.userName.next(sessionStorage.getItem('username')); this.redirectToPage(); } @@ -110,6 +118,13 @@ export class AuthenticationService { return this.changePassword.asObservable(); } + /** + * Get method for Observable forgotssword + */ + get isForgotPassword(): Observable { + return this.forgotPassword.asObservable(); + } + /** * Get method for Observable Username */ @@ -136,9 +151,9 @@ export class AuthenticationService { return this.restService.postResource(apiURLHeader, this.payLoad) .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); + sessionStorage.setItem('firstLogin', 'true'); + sessionStorage.setItem('id_token', data.id); + sessionStorage.setItem('user_id', data.user_id); this.idle.watch(true); this.changePassword.next(true); this.loggedIn.next(false); @@ -174,27 +189,37 @@ export class AuthenticationService { /** set local storage on auth process @public */ public setLocalStorage(data: ProjectModel): void { - localStorage.setItem('id_token', data.id); - localStorage.setItem('expires', data.expires.toString()); - localStorage.setItem('username', data.username); - localStorage.setItem('isAdmin', (data.admin) ? 'true' : 'false'); - localStorage.setItem('project_id', data.project_id); - localStorage.setItem('project', data.project_name); - localStorage.setItem('token_state', data.id); + if (!isNullOrUndefined(data.timeout)) { + sessionStorage.setItem('timeout', data.timeout); + } + sessionStorage.setItem('id_token', data.id); + sessionStorage.setItem('expires', data.expires.toString()); + sessionStorage.setItem('username', data.username); + sessionStorage.setItem('isAdmin', (data.admin) ? 'true' : 'false'); + sessionStorage.setItem('project_id', data.project_id); + sessionStorage.setItem('project', data.project_name); + sessionStorage.setItem('token_state', data.id); + sessionStorage.setItem('user_id', data.user_id); + sessionStorage.setItem('user_show', String(data.user_show)); + sessionStorage.setItem('admin_show', String(data.admin_show)); + sessionStorage.setItem('last_login', this.sharedService.convertEpochTime(!isNullOrUndefined(data.last_login) ? data.last_login : null)); + sessionStorage.setItem('failed_count', data.login_count); this.projectName$.next(data.project_name); } /** 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); + const langCode: string = sessionStorage.getItem('languageCode'); + const redirecturl: string = isNullOrUndefined(sessionStorage.getItem('returnUrl')) ? '/' : sessionStorage.getItem('returnUrl'); + const osmVersion: string = isNullOrUndefined(sessionStorage.getItem('osmVersion')) ? '' : sessionStorage.getItem('osmVersion'); + const timeOut: string = isNullOrUndefined(sessionStorage.getItem('timeout')) ? '1200' : sessionStorage.getItem('timeout'); + sessionStorage.clear(); + sessionStorage.setItem('languageCode', langCode); + sessionStorage.setItem('returnUrl', redirecturl); + sessionStorage.setItem('token_state', null); + sessionStorage.setItem('osmVersion', osmVersion); + sessionStorage.setItem('timeout', timeOut); this.idle.stop(); this.router.navigate(['login']).catch((): void => { // Catch Navigation Error @@ -205,13 +230,13 @@ export class AuthenticationService { */ public logout(): void { this.returnUrl = this.router.url; - localStorage.setItem('returnUrl', this.returnUrl); + sessionStorage.setItem('returnUrl', this.returnUrl); this.modalService.dismissAll(); this.destoryToken(); } /** Destory tokens on logout @public */ public destoryToken(): void { - const tokenID: string = localStorage.getItem('id_token'); + const tokenID: string = sessionStorage.getItem('id_token'); if (tokenID !== null) { const deletingURl: string = environment.GENERATETOKEN_URL + '/' + tokenID; this.restService.deleteResource(deletingURl).subscribe((res: {}): void => { @@ -224,9 +249,9 @@ export class AuthenticationService { /** Return to previous page deny access to changepassword */ public redirectToPage(): void { - if (window.location.pathname === '/changepassword' && localStorage.getItem('username') !== null) { + if (window.location.pathname === '/changepassword' && sessionStorage.getItem('username') !== null) { window.history.back(); - } else if (window.location.pathname === '/' && localStorage.getItem('firstLogin') === 'true') { + } else if (window.location.pathname === '/' && sessionStorage.getItem('firstLogin') === 'true') { this.router.navigate(['/login']).catch((): void => { // Catch Navigation Error });