[angular-16-irish-monitor | DONE | CHANGED] make session redirect urls an array, allow double redirect to fix page and then to current page (when redirected to login)

This commit is contained in:
argirok 2024-01-09 10:07:04 +02:00
parent 54a7ad165e
commit bc259c7c3c
3 changed files with 27 additions and 18 deletions

View File

@ -68,21 +68,32 @@ export class Session {
var cookie = COOKIE.getCookie(COOKIE.cookieName_id);
return (cookie != null && cookie != "");
}
public static clearReloadUrl() {
COOKIE.setCookie("reloadURLs", JSON.stringify([]), -1);
}
public static setReloadUrl(host: string, path: string, params: string, fragment: string) {
var URL = {};
let URLs:any[] = this.getReloadUrl();
let URL = {};
URL["host"] = host;
URL["path"] = path;
URL["params"] = params;
URL["fragment"] = fragment;
COOKIE.setCookie("reloadURL", JSON.stringify(URL), -1);
URLs.push(URL);
COOKIE.setCookie("reloadURLs", JSON.stringify(URLs), -1);
}
public static getReloadUrl() {
var URL = COOKIE.getCookie("reloadURL");
URL = JSON.parse(URL);
return URL;
let URLs = COOKIE.getCookie("reloadURLs");
let array = JSON.parse(URLs);
return array?array:[];
}
public static popReloadUrl() {
let array = this.getReloadUrl();
let Url = array.length>0?array[0]:null;
COOKIE.setCookie("reloadURLs", JSON.stringify(array.slice(1)), -1);
return Url;
}
public static getParamsObj(params: string) {

View File

@ -15,7 +15,7 @@ export class ReloadComponent {
}
public ngOnInit() {
let URL = Session.getReloadUrl();
let URL = Session.popReloadUrl();
if (URL && URL["path"] && URL["path"] != "") {
let url: string = URL["path"];
let host = URL["host"];
@ -24,14 +24,12 @@ export class ReloadComponent {
let baseUrl = (document && document.getElementsByTagName('base')) ? document.getElementsByTagName('base')[0].href.split(document.location.host)[1] : "/";
url = (baseUrl.length > 1 && url.indexOf(baseUrl) != -1) ? ("/" + url.split(baseUrl)[1]) : url;
if (paramsObject) {
Session.setReloadUrl("", "", "", "");
if(URL['fragment'] && URL['fragment'] !== '') {
this._router.navigate([url], {queryParams: paramsObject, fragment: URL['fragment']});
} else {
this._router.navigate([url], {queryParams: paramsObject});
}
} else {
Session.setReloadUrl("", "", "", "");
if(URL['fragment'] && URL['fragment'] !== '') {
this._router.navigate([url], {fragment: URL['fragment']});
} else {
@ -39,12 +37,10 @@ export class ReloadComponent {
}
}
} else {
Session.setReloadUrl("", "", "", "");
window.location.href = host + url + ((URL["params"] && URL["params"] != null) ? ((URL["params"].indexOf("?") == -1 ? "?" : "") + URL["params"]) : "");
}
} else {
Session.setReloadUrl("", "", "", "");
this._router.navigate(['/']);
}
}

View File

@ -18,6 +18,7 @@ export class UserManagementService {
private static LOGOUT: string = 'openid_logout';
private static USERINFO: string = 'userInfo';
public fixRedirectURL: string = null;
public allowDoubleRedirectToFixAndCurrentPage: boolean = false;
private redirectUrl: string = null;
private subscription;
private readonly routerSubscription;
@ -53,7 +54,7 @@ export class UserManagementService {
public updateUserInfo(resolve: Function = null) {
this.subscription = this.getUserInfoAt().subscribe(user => {
console.log(user)
// console.log(user)
this.getUserInfoSubject.next(user);
if (resolve) {
resolve();
@ -66,8 +67,9 @@ export class UserManagementService {
});
}
public setRedirectUrl(url: string = null) {
if (url) {
public setRedirectUrl(redirectTo: string | string [] = null) {
let urls = Array.isArray(redirectTo)?redirectTo:(redirectTo?[redirectTo]:[])
for(let url of urls) {
let parts = url.split('?');
let path = properties.baseLink + parts[0];
let params = null;
@ -85,7 +87,8 @@ export class UserManagementService {
Session.setReloadUrl(location.protocol + "//" + location.host, path, params, fragment);
}
this.redirectUrl = StringUtils.URIEncode(location.protocol + "//" + location.host + this.fixRedirectURL);
} else {
}
if(urls.length == 0){
this.redirectUrl = StringUtils.URIEncode(location.href);
Session.setReloadUrl(location.protocol + "//" + location.host, location.pathname, location.search, location.hash);
}
@ -95,15 +98,14 @@ export class UserManagementService {
if(redirect) {
this.redirectUrl = redirect;
} else if (this.fixRedirectURL) {
console.log(this.fixRedirectURL)
this.setRedirectUrl(this.fixRedirectURL);
this.setRedirectUrl(this.allowDoubleRedirectToFixAndCurrentPage?[this.fixRedirectURL,location.href.split(location.host)[1]]:this.fixRedirectURL);
} else {
this.setRedirectUrl();
}
let loginURL: string | string[] = isArray(properties.loginServiceURL)?properties.loginServiceURL.map(url => url + UserManagementService.LOGIN):(properties.loginServiceURL + UserManagementService.LOGIN);
window.location.href = this.setURL(loginURL) + this.redirectUrl;
}
public logout() {
this.setRedirectUrl();
Session.removeUser();