Add redirect url in login/logout. Add new userInfo method in getUserInfo

This commit is contained in:
Konstantinos Triantafyllou 2021-11-05 13:29:54 +02:00
parent 104c54a3e8
commit 0aa44646b1
2 changed files with 30 additions and 26 deletions

View File

@ -1,5 +1,3 @@
import {StringUtils} from "../../utils/string-utils.class";
export class User { export class User {
email: string; email: string;
firstname: string; firstname: string;
@ -15,8 +13,6 @@ export class User {
export class Session { export class Session {
public static removeUser() { public static removeUser() {
COOKIE.deleteCookie(COOKIE.cookieName_id); COOKIE.deleteCookie(COOKIE.cookieName_id);
//COOKIE.deleteCookie("openAIRESession");
COOKIE.deleteCookie("openAIREUser");
} }
public static isLoggedIn(): boolean { public static isLoggedIn(): boolean {

View File

@ -6,14 +6,16 @@ import {map} from "rxjs/operators";
import {NavigationEnd, Router} from "@angular/router"; import {NavigationEnd, Router} from "@angular/router";
import {properties} from "../../../environments/environment"; import {properties} from "../../../environments/environment";
import {StringUtils} from "../utils/string-utils.class"; import {StringUtils} from "../utils/string-utils.class";
import {CustomOptions} from "./servicesUtils/customOptions.class";
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
export class UserManagementService{ export class UserManagementService {
private getUserInfoSubject: BehaviorSubject<User> = new BehaviorSubject<User>(null); private getUserInfoSubject: BehaviorSubject<User> = new BehaviorSubject<User>(null);
public fixRedirectURL: string = null; public fixRedirectURL: string = null;
private redirectUrl: string = null;
private readonly promise: Promise<User>; private readonly promise: Promise<User>;
sub; sub;
routeSub; routeSub;
@ -34,10 +36,10 @@ export class UserManagementService{
} }
clearSubscriptions() { clearSubscriptions() {
if(this.routeSub){ if (this.routeSub) {
this.routeSub.unsubscribe(); this.routeSub.unsubscribe();
} }
if(this.sub){ if (this.sub) {
this.sub.unsubscribe(); this.sub.unsubscribe();
} }
} }
@ -51,32 +53,36 @@ export class UserManagementService{
} }
public updateUserInfo(resolve: Function = null) { public updateUserInfo(resolve: Function = null) {
/** @deprecated **/
const token = COOKIE.getCookie('AccessToken'); const token = COOKIE.getCookie('AccessToken');
if (!token) { if (!token && properties.userInfoUrl.includes("accessToken")) {
this.getUserInfoSubject.next(null); this.getUserInfoSubject.next(null);
if(resolve) { if (resolve) {
resolve(); resolve();
} }
} else { } else {
this.sub = this.http.get<User>(properties.userInfoUrl + token).pipe(map(userInfo => { 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); return this.parseUserInfo(userInfo);
})).subscribe(user => { })).subscribe(user => {
this.getUserInfoSubject.next(user); this.getUserInfoSubject.next(user);
if(resolve) { if (resolve) {
resolve(); resolve();
} }
}, error => { }, error => {
this.getUserInfoSubject.next(null); if(this.getUserInfoSubject.value) {
if(resolve) { this.getUserInfoSubject.next(null);
resolve(); }
} if (resolve) {
}); resolve();
}
});
} }
} }
private async getUserInfoAsync(): Promise<User> { private async getUserInfoAsync(): Promise<User> {
await this.promise; await this.promise;
if(this.sub){ if (this.sub) {
this.sub.unsubscribe(); this.sub.unsubscribe();
} }
return this.getUserInfoSubject.getValue(); return this.getUserInfoSubject.getValue();
@ -105,7 +111,7 @@ export class UserManagementService{
info.roles.forEach(role => { info.roles.forEach(role => {
user.role.push(role); user.role.push(role);
}); });
}else{ } else {
if (info.edu_person_entitlements) { if (info.edu_person_entitlements) {
user.role = info.edu_person_entitlements; user.role = info.edu_person_entitlements;
} }
@ -115,7 +121,7 @@ export class UserManagementService{
} }
public setRedirectUrl(url: string = null) { public setRedirectUrl(url: string = null) {
if(url) { if (url) {
let parts = url.split('?'); let parts = url.split('?');
let path = properties.baseLink + parts[0]; let path = properties.baseLink + parts[0];
let params = null; let params = null;
@ -123,34 +129,36 @@ export class UserManagementService{
params = parts[1]; params = parts[1];
} }
let hash = path.indexOf("#"); let hash = path.indexOf("#");
let fragment = (hash !== -1)?path.slice(hash + 1):null; let fragment = (hash !== -1) ? path.slice(hash + 1) : null;
if(fragment) { if (fragment) {
path = path.slice(0, hash); path = path.slice(0, hash);
} else { } else {
fragment = ""; fragment = "";
} }
if(!path.includes('/reload')) { if (!path.includes('/reload')) {
Session.setReloadUrl(location.protocol + "//" + location.host, path, params, fragment); Session.setReloadUrl(location.protocol + "//" + location.host, path, params, fragment);
} }
this.redirectUrl = StringUtils.URIEncode(location.protocol + "//" + location.host + this.fixRedirectURL);
} else { } else {
this.redirectUrl = StringUtils.URIEncode(location.href);
Session.setReloadUrl(location.protocol + "//" + location.host, location.pathname, location.search, location.hash); Session.setReloadUrl(location.protocol + "//" + location.host, location.pathname, location.search, location.hash);
} }
} }
public login() { public login() {
if(this.fixRedirectURL) { if (this.fixRedirectURL) {
this.setRedirectUrl(this.fixRedirectURL); this.setRedirectUrl(this.fixRedirectURL);
} else { } else {
this.setRedirectUrl(); this.setRedirectUrl();
} }
window.location.href = properties.loginUrl; window.location.href = properties.loginUrl + "?redirect=" + this.redirectUrl;
} }
public logout() { public logout() {
this.setRedirectUrl(); this.setRedirectUrl();
Session.removeUser(); Session.removeUser();
if (properties.logoutUrl.includes('openid_logout')) { if (properties.logoutUrl.includes('openid_logout')) {
window.location.href = properties.logoutUrl; window.location.href = properties.logoutUrl + "?redirect=" + this.redirectUrl;
} else { } else {
window.location.href = properties.logoutUrl + StringUtils.URIEncode(location.href); window.location.href = properties.logoutUrl + StringUtils.URIEncode(location.href);
} }