From 131327a326610543ed1b26db3b0b4c49e8b83b47 Mon Sep 17 00:00:00 2001 From: "k.triantafyllou" Date: Wed, 13 Apr 2022 12:24:14 +0300 Subject: [PATCH] 1. Make AdvancedAsyncSubject available in all envrionments and remove condition about existance of AccessToken. 2. Deprecated is LoggedIn method. --- login/utils/helper.class.ts | 8 ++- services/servicesUtils/customOptions.class.ts | 17 +++-- services/user-management.service.ts | 70 +++++-------------- 3 files changed, 33 insertions(+), 62 deletions(-) diff --git a/login/utils/helper.class.ts b/login/utils/helper.class.ts index 569351f2..63f5a7c6 100644 --- a/login/utils/helper.class.ts +++ b/login/utils/helper.class.ts @@ -15,6 +15,12 @@ export class Session { COOKIE.deleteCookie(COOKIE.cookieName_id); } + /** + * @deprecated + * + * Use userInfoSubject @UserManagementService in order to check if user is logged in + * + * */ public static isLoggedIn(): boolean { var cookie = COOKIE.getCookie(COOKIE.cookieName_id); return (cookie != null && cookie != ""); @@ -29,7 +35,7 @@ export class Session { COOKIE.setCookie("reloadURL", JSON.stringify(URL), -1); } - public static getReloadUrl(plainText: boolean = false) { + public static getReloadUrl() { var URL = COOKIE.getCookie("reloadURL"); URL = JSON.parse(URL); return URL; diff --git a/services/servicesUtils/customOptions.class.ts b/services/servicesUtils/customOptions.class.ts index a7e55ead..39194da6 100644 --- a/services/servicesUtils/customOptions.class.ts +++ b/services/servicesUtils/customOptions.class.ts @@ -4,12 +4,12 @@ import {HttpHeaders} from "@angular/common/http"; export class CustomOptions { - public static registryOptions(): {} { - return { - headers: new HttpHeaders({ - 'Content-Type': 'application/json', - }), withCredentials: true - }; + public static registryOptions(body = true): {} { + let httpHeaders = new HttpHeaders(); + if(body) { + httpHeaders.set('Content-Type', 'application/json'); + } + return {headers: httpHeaders, withCredentials: true}; } public static getAuthOptionsWithBody():{} { @@ -22,12 +22,11 @@ export class CustomOptions { } public static getAuthOptions():{} { - const httpOptions = { + return { headers: new HttpHeaders({ - 'X-XSRF-TOKEN': (COOKIE.getCookie(COOKIE.cookieName_id))?COOKIE.getCookie(COOKIE.cookieName_id):'' + 'X-XSRF-TOKEN': (COOKIE.getCookie(COOKIE.cookieName_id)) ? COOKIE.getCookie(COOKIE.cookieName_id) : '' }), withCredentials: true }; - return httpOptions; } } diff --git a/services/user-management.service.ts b/services/user-management.service.ts index 96f61ec1..73ea818c 100644 --- a/services/user-management.service.ts +++ b/services/user-management.service.ts @@ -13,40 +13,21 @@ import {AdvancedAsyncSubject} from "../utils/AdvancedAsyncSubject"; providedIn: 'root' }) export class UserManagementService { - - private getUserInfoSubject: AdvancedAsyncSubject | BehaviorSubject; + private readonly getUserInfoSubject: AdvancedAsyncSubject = new AdvancedAsyncSubject(); public fixRedirectURL: string = null; private redirectUrl: string = null; private readonly promise: Promise; - sub; - routeSub; + private subscription; - constructor(private http: HttpClient, - private router: Router) { - if(properties.environment === "development") { - this.getUserInfoSubject = new AdvancedAsyncSubject(); - } else { - this.getUserInfoSubject = new BehaviorSubject(null) - } + constructor(private http: HttpClient) { this.promise = new Promise((resolve => { this.updateUserInfo(resolve); })); - this.routeSub = this.router.events.subscribe(event => { - if (event instanceof NavigationEnd) { - const token = COOKIE.getCookie('AccessToken'); - if (!token && this.getUserInfoSubject.getValue() !== null) { - this.getUserInfoSubject.next(null); - } - } - }); } clearSubscriptions() { - if (this.routeSub) { - this.routeSub.unsubscribe(); - } - if (this.sub) { - this.sub.unsubscribe(); + if (this.subscription) { + this.subscription.unsubscribe(); } } @@ -59,43 +40,32 @@ export class UserManagementService { } public updateUserInfo(resolve: Function = null) { - const token = COOKIE.getCookie('AccessToken'); - if (!token) { + this.subscription = this.http.get(properties.userInfoUrl, CustomOptions.registryOptions()).pipe(map(userInfo => { + return this.parseUserInfo(userInfo); + })).subscribe(user => { + this.getUserInfoSubject.next(user); + if (resolve) { + resolve(); + } + }, error => { this.getUserInfoSubject.next(null); if (resolve) { resolve(); } - } else { - let userInfoUrl = (properties.userInfoUrl.includes("accessToken")?(properties.userInfoUrl + token):properties.userInfoUrl); - this.sub = this.http.get(userInfoUrl, CustomOptions.registryOptions()).pipe(map(userInfo => { - return this.parseUserInfo(userInfo); - })).subscribe(user => { - this.getUserInfoSubject.next(user); - if (resolve) { - resolve(); - } - }, error => { - if(this.getUserInfoSubject.getValue() || this.getUserInfoSubject instanceof AdvancedAsyncSubject) { - this.getUserInfoSubject.next(null); - } - if (resolve) { - resolve(); - } - }); - } + }); } private async getUserInfoAsync(): Promise { await this.promise; - if (this.sub) { - this.sub.unsubscribe(); + if (this.subscription) { + this.subscription.unsubscribe(); } return this.getUserInfoSubject.getValue(); } private parseUserInfo(info: any) { const user: User = new User(); - user.id = (info.sub && info.sub.indexOf('@')) ? info.sub.substring(0, info.sub.indexOf('@')) : info.sub; + user.id = (info.subscription && info.subscription.indexOf('@')) ? info.subscription.substring(0, info.subscription.indexOf('@')) : info.subscription; user.firstname = (info.given_name) ? info.given_name : ""; user.lastname = (info.family_name) ? info.family_name : ""; user.email = info.email.toLowerCase(); // TODO remove, is a quick fix @@ -162,10 +132,6 @@ export class UserManagementService { public logout() { this.setRedirectUrl(); Session.removeUser(); - if (properties.logoutUrl.includes('openid_logout')) { - window.location.href = properties.logoutUrl + "?redirect=" + this.redirectUrl; - } else { - window.location.href = properties.logoutUrl + StringUtils.URIEncode(location.href); - } + window.location.href = properties.logoutUrl + "?redirect=" + this.redirectUrl; } }