1. Make AdvancedAsyncSubject available in all envrionments and remove condition about existance of AccessToken. 2. Deprecated is LoggedIn method.

This commit is contained in:
Konstantinos Triantafyllou 2022-04-13 12:24:14 +03:00
parent d0dc3bd5e8
commit 131327a326
3 changed files with 33 additions and 62 deletions

View File

@ -15,6 +15,12 @@ export class Session {
COOKIE.deleteCookie(COOKIE.cookieName_id); COOKIE.deleteCookie(COOKIE.cookieName_id);
} }
/**
* @deprecated
*
* Use userInfoSubject @UserManagementService in order to check if user is logged in
*
* */
public static isLoggedIn(): boolean { public static isLoggedIn(): boolean {
var cookie = COOKIE.getCookie(COOKIE.cookieName_id); var cookie = COOKIE.getCookie(COOKIE.cookieName_id);
return (cookie != null && cookie != ""); return (cookie != null && cookie != "");
@ -29,7 +35,7 @@ export class Session {
COOKIE.setCookie("reloadURL", JSON.stringify(URL), -1); COOKIE.setCookie("reloadURL", JSON.stringify(URL), -1);
} }
public static getReloadUrl(plainText: boolean = false) { public static getReloadUrl() {
var URL = COOKIE.getCookie("reloadURL"); var URL = COOKIE.getCookie("reloadURL");
URL = JSON.parse(URL); URL = JSON.parse(URL);
return URL; return URL;

View File

@ -4,12 +4,12 @@ import {HttpHeaders} from "@angular/common/http";
export class CustomOptions { export class CustomOptions {
public static registryOptions(): {} { public static registryOptions(body = true): {} {
return { let httpHeaders = new HttpHeaders();
headers: new HttpHeaders({ if(body) {
'Content-Type': 'application/json', httpHeaders.set('Content-Type', 'application/json');
}), withCredentials: true }
}; return {headers: httpHeaders, withCredentials: true};
} }
public static getAuthOptionsWithBody():{} { public static getAuthOptionsWithBody():{} {
@ -22,12 +22,11 @@ export class CustomOptions {
} }
public static getAuthOptions():{} { public static getAuthOptions():{} {
const httpOptions = { return {
headers: new HttpHeaders({ 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 }), withCredentials: true
}; };
return httpOptions;
} }
} }

View File

@ -13,40 +13,21 @@ import {AdvancedAsyncSubject} from "../utils/AdvancedAsyncSubject";
providedIn: 'root' providedIn: 'root'
}) })
export class UserManagementService { export class UserManagementService {
private readonly getUserInfoSubject: AdvancedAsyncSubject<User> = new AdvancedAsyncSubject<User>();
private getUserInfoSubject: AdvancedAsyncSubject<User> | BehaviorSubject<User>;
public fixRedirectURL: string = null; public fixRedirectURL: string = null;
private redirectUrl: string = null; private redirectUrl: string = null;
private readonly promise: Promise<User>; private readonly promise: Promise<User>;
sub; private subscription;
routeSub;
constructor(private http: HttpClient, constructor(private http: HttpClient) {
private router: Router) {
if(properties.environment === "development") {
this.getUserInfoSubject = new AdvancedAsyncSubject<User>();
} else {
this.getUserInfoSubject = new BehaviorSubject<User>(null)
}
this.promise = new Promise<any>((resolve => { this.promise = new Promise<any>((resolve => {
this.updateUserInfo(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() { clearSubscriptions() {
if (this.routeSub) { if (this.subscription) {
this.routeSub.unsubscribe(); this.subscription.unsubscribe();
}
if (this.sub) {
this.sub.unsubscribe();
} }
} }
@ -59,43 +40,32 @@ export class UserManagementService {
} }
public updateUserInfo(resolve: Function = null) { public updateUserInfo(resolve: Function = null) {
const token = COOKIE.getCookie('AccessToken'); this.subscription = this.http.get<User>(properties.userInfoUrl, CustomOptions.registryOptions()).pipe(map(userInfo => {
if (!token) { return this.parseUserInfo(userInfo);
})).subscribe(user => {
this.getUserInfoSubject.next(user);
if (resolve) {
resolve();
}
}, error => {
this.getUserInfoSubject.next(null); this.getUserInfoSubject.next(null);
if (resolve) { if (resolve) {
resolve(); resolve();
} }
} else { });
let userInfoUrl = (properties.userInfoUrl.includes("accessToken")?(properties.userInfoUrl + token):properties.userInfoUrl);
this.sub = this.http.get<User>(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<User> { private async getUserInfoAsync(): Promise<User> {
await this.promise; await this.promise;
if (this.sub) { if (this.subscription) {
this.sub.unsubscribe(); this.subscription.unsubscribe();
} }
return this.getUserInfoSubject.getValue(); return this.getUserInfoSubject.getValue();
} }
private parseUserInfo(info: any) { private parseUserInfo(info: any) {
const user: User = new User(); 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.firstname = (info.given_name) ? info.given_name : "";
user.lastname = (info.family_name) ? info.family_name : ""; user.lastname = (info.family_name) ? info.family_name : "";
user.email = info.email.toLowerCase(); // TODO remove, is a quick fix user.email = info.email.toLowerCase(); // TODO remove, is a quick fix
@ -162,10 +132,6 @@ export class UserManagementService {
public logout() { public logout() {
this.setRedirectUrl(); this.setRedirectUrl();
Session.removeUser(); Session.removeUser();
if (properties.logoutUrl.includes('openid_logout')) { window.location.href = properties.logoutUrl + "?redirect=" + this.redirectUrl;
window.location.href = properties.logoutUrl + "?redirect=" + this.redirectUrl;
} else {
window.location.href = properties.logoutUrl + StringUtils.URIEncode(location.href);
}
} }
} }