[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); var cookie = COOKIE.getCookie(COOKIE.cookieName_id);
return (cookie != null && cookie != ""); return (cookie != null && cookie != "");
} }
public static clearReloadUrl() {
COOKIE.setCookie("reloadURLs", JSON.stringify([]), -1);
}
public static setReloadUrl(host: string, path: string, params: string, fragment: string) { public static setReloadUrl(host: string, path: string, params: string, fragment: string) {
var URL = {}; let URLs:any[] = this.getReloadUrl();
let URL = {};
URL["host"] = host; URL["host"] = host;
URL["path"] = path; URL["path"] = path;
URL["params"] = params; URL["params"] = params;
URL["fragment"] = fragment; URL["fragment"] = fragment;
COOKIE.setCookie("reloadURL", JSON.stringify(URL), -1); URLs.push(URL);
COOKIE.setCookie("reloadURLs", JSON.stringify(URLs), -1);
} }
public static getReloadUrl() { public static getReloadUrl() {
var URL = COOKIE.getCookie("reloadURL"); let URLs = COOKIE.getCookie("reloadURLs");
URL = JSON.parse(URL); let array = JSON.parse(URLs);
return URL; 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) { public static getParamsObj(params: string) {

View File

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

View File

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