From 9a0fcf59824af34825a05f8c3fe74139bf180e8a Mon Sep 17 00:00:00 2001 From: rmartinc Date: Tue, 18 Jun 2024 16:25:11 +0200 Subject: [PATCH] Remove the timeout in authChecker when page is unloaded Closes #30334 Signed-off-by: rmartinc --- .../base/login/resources/js/authChecker.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/themes/src/main/resources/theme/base/login/resources/js/authChecker.js b/themes/src/main/resources/theme/base/login/resources/js/authChecker.js index c5938c3e89..51e2579e00 100644 --- a/themes/src/main/resources/theme/base/login/resources/js/authChecker.js +++ b/themes/src/main/resources/theme/base/login/resources/js/authChecker.js @@ -1,6 +1,17 @@ const CHECK_INTERVAL_MILLISECS = 2000; const initialSession = getSession(); +let timeout; + +// Remove the timeout when unloading to avoid execution of the +// checkCookiesAndSetTimer when the page is already submitted +addEventListener("beforeunload", () => { + if (timeout) { + clearTimeout(timeout); + timeout = undefined; + } +}); + export function checkCookiesAndSetTimer(loginRestartUrl) { if (initialSession) { // We started with a session, so there is nothing to do, exit. @@ -11,9 +22,9 @@ export function checkCookiesAndSetTimer(loginRestartUrl) { if (!session) { // The session is not present, check again later. - setTimeout( + timeout = setTimeout( () => checkCookiesAndSetTimer(loginRestartUrl), - CHECK_INTERVAL_MILLISECS + CHECK_INTERVAL_MILLISECS, ); } else { // Redirect to the login restart URL. This can typically automatically login user due the SSO @@ -29,7 +40,9 @@ function getCookieByName(name) { for (const cookie of document.cookie.split(";")) { const [key, value] = cookie.split("=").map((value) => value.trim()); if (key === name) { - return value.startsWith('"') && value.endsWith('"') ? value.slice(1, -1) : value; + return value.startsWith('"') && value.endsWith('"') + ? value.slice(1, -1) + : value; } } return null;