Remove the timeout in authChecker when page is unloaded

Closes #30334

Signed-off-by: rmartinc <rmartinc@redhat.com>
This commit is contained in:
rmartinc 2024-06-18 16:25:11 +02:00 committed by Alexander Schwartz
parent c48e7bc24c
commit 9a0fcf5982

View file

@ -1,6 +1,17 @@
const CHECK_INTERVAL_MILLISECS = 2000; const CHECK_INTERVAL_MILLISECS = 2000;
const initialSession = getSession(); 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) { export function checkCookiesAndSetTimer(loginRestartUrl) {
if (initialSession) { if (initialSession) {
// We started with a session, so there is nothing to do, exit. // We started with a session, so there is nothing to do, exit.
@ -11,9 +22,9 @@ export function checkCookiesAndSetTimer(loginRestartUrl) {
if (!session) { if (!session) {
// The session is not present, check again later. // The session is not present, check again later.
setTimeout( timeout = setTimeout(
() => checkCookiesAndSetTimer(loginRestartUrl), () => checkCookiesAndSetTimer(loginRestartUrl),
CHECK_INTERVAL_MILLISECS CHECK_INTERVAL_MILLISECS,
); );
} else { } else {
// Redirect to the login restart URL. This can typically automatically login user due the SSO // 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(";")) { for (const cookie of document.cookie.split(";")) {
const [key, value] = cookie.split("=").map((value) => value.trim()); const [key, value] = cookie.split("=").map((value) => value.trim());
if (key === name) { 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; return null;