From f3c10a4ca2d6d695255736909485bfaa89e46ba5 Mon Sep 17 00:00:00 2001 From: Igor Kulikov Date: Tue, 11 May 2021 18:03:18 +0300 Subject: [PATCH] Improve query params handling --- ui-ngx/src/app/core/auth/auth.service.ts | 5 +++-- ui-ngx/src/app/core/services/utils.service.ts | 19 ++++++++++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/ui-ngx/src/app/core/auth/auth.service.ts b/ui-ngx/src/app/core/auth/auth.service.ts index 37802a08ed..4d25e5ce72 100644 --- a/ui-ngx/src/app/core/auth/auth.service.ts +++ b/ui-ngx/src/app/core/auth/auth.service.ts @@ -294,10 +294,11 @@ export class AuthService { }) ); } else if (accessToken) { - this.utils.updateQueryParam('accessToken', null); + const queryParamsToRemove = ['accessToken']; if (refreshToken) { - this.utils.updateQueryParam('refreshToken', null); + queryParamsToRemove.push('refreshToken'); } + this.utils.removeQueryParams(queryParamsToRemove); try { this.updateAndValidateToken(accessToken, 'jwt_token', false); if (refreshToken) { diff --git a/ui-ngx/src/app/core/services/utils.service.ts b/ui-ngx/src/app/core/services/utils.service.ts index 3c1c659e95..f8a4ad279b 100644 --- a/ui-ngx/src/app/core/services/utils.service.ts +++ b/ui-ngx/src/app/core/services/utils.service.ts @@ -410,9 +410,23 @@ export class UtilsService { return decodeURIComponent(results[2].replace(/\+/g, ' ')); } + public removeQueryParams(keys: Array) { + let params = this.window.location.search; + for (const key of keys) { + params = this.updateUrlQueryString(params, key, null); + } + const baseUrlPart = [baseUrl(), this.window.location.pathname].join(''); + this.window.history.replaceState({}, '', baseUrlPart + params); + } + public updateQueryParam(name: string, value: string | null) { const baseUrlPart = [baseUrl(), this.window.location.pathname].join(''); const urlQueryString = this.window.location.search; + const params = this.updateUrlQueryString(urlQueryString, name, value); + this.window.history.replaceState({}, '', baseUrlPart + params); + } + + private updateUrlQueryString(urlQueryString: string, name: string, value: string | null): string { let newParam = ''; let params = ''; if (value !== null) { @@ -425,13 +439,16 @@ export class UtilsService { newParam = '$1' + newParam; } params = urlQueryString.replace(keyRegex, newParam); + if (params.startsWith('&')) { + params = '?' + params.substring(1); + } } else if (newParam) { params = urlQueryString + '&' + newParam; } } else if (newParam) { params = '?' + newParam; } - this.window.history.replaceState({}, '', baseUrlPart + params); + return params; } public baseUrl(): string {