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);
}
/**
* @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;

View File

@ -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;
}
}

View File

@ -13,40 +13,21 @@ import {AdvancedAsyncSubject} from "../utils/AdvancedAsyncSubject";
providedIn: 'root'
})
export class UserManagementService {
private getUserInfoSubject: AdvancedAsyncSubject<User> | BehaviorSubject<User>;
private readonly getUserInfoSubject: AdvancedAsyncSubject<User> = new AdvancedAsyncSubject<User>();
public fixRedirectURL: string = null;
private redirectUrl: string = null;
private readonly promise: Promise<User>;
sub;
routeSub;
private subscription;
constructor(private http: HttpClient,
private router: Router) {
if(properties.environment === "development") {
this.getUserInfoSubject = new AdvancedAsyncSubject<User>();
} else {
this.getUserInfoSubject = new BehaviorSubject<User>(null)
}
constructor(private http: HttpClient) {
this.promise = new Promise<any>((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<User>(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<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> {
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;
}
}