diff --git a/src/routes/(app)/+layout.svelte b/src/routes/(app)/+layout.svelte index 6bec3dde60..e00070f8ef 100644 --- a/src/routes/(app)/+layout.svelte +++ b/src/routes/(app)/+layout.svelte @@ -227,9 +227,14 @@ }); }; + const gotoAuth = async () => { + const currentUrl = `${$page.url.pathname}${$page.url.search}`; + await goto(`/auth?redirect=${encodeURIComponent(currentUrl)}`); + }; + onMount(async () => { if ($user === undefined || $user === null) { - await goto('/auth'); + await gotoAuth(); return; } if (!['user', 'admin'].includes($user?.role)) { @@ -392,6 +397,10 @@ void openSettingsFromUrl(); } + $: if (loaded && ($user === undefined || $user === null)) { + void gotoAuth(); + } + const checkForVersionUpdates = async () => { version = await getVersionUpdates(localStorage.token).catch((error) => { return { diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 422902249c..c7fb59b6fe 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -837,8 +837,8 @@ } }; - const redirectToAuthAfterUnauthorized = () => { - if (isAuthRedirectInProgress || window.location.pathname === '/auth') { + const clearExpiredSession = () => { + if (isAuthRedirectInProgress) { return; } @@ -855,11 +855,7 @@ console.error('Error signing out expired session:', error); }); toast.error($i18n.t('Session expired. Please sign in again.')); - - const currentPath = `${window.location.pathname}${window.location.search}`; - goto(`/auth?redirect=${encodeURIComponent(currentPath)}`).finally(() => { - isAuthRedirectInProgress = false; - }); + isAuthRedirectInProgress = false; }; const isCurrentSessionUnauthorized = async (originalFetch) => { @@ -885,7 +881,7 @@ } if (now >= exp - TOKEN_EXPIRY_BUFFER) { - redirectToAuthAfterUnauthorized(); + clearExpiredSession(); } }; @@ -1003,7 +999,7 @@ isAuthenticatedBackendFetch(input, init) && (await isCurrentSessionUnauthorized(originalFetch)) ) { - redirectToAuthAfterUnauthorized(); + clearExpiredSession(); } return response; @@ -1189,9 +1185,6 @@ if ($config) { await setupSocket($config.features?.enable_websocket ?? true); - const currentUrl = `${window.location.pathname}${window.location.search}`; - const encodedUrl = encodeURIComponent(currentUrl); - if (localStorage.token) { // Get Session User Info const sessionUser = await getSessionUser(localStorage.token).catch((error) => { @@ -1223,15 +1216,8 @@ .catch(() => {}); } } else { - // Redirect Invalid Session User to /auth Page localStorage.removeItem('token'); - await goto(`/auth?redirect=${encodedUrl}`); - } - } else { - // Don't redirect if we're already on the auth page - // Needed because we pass in tokens from OAuth logins via URL fragments - if ($page.url.pathname !== '/auth') { - await goto(`/auth?redirect=${encodedUrl}`); + await user.set(null); } } } diff --git a/src/routes/s/[id]/+page.svelte b/src/routes/s/[id]/+page.svelte index c1f300b0e3..3341e7e425 100644 --- a/src/routes/s/[id]/+page.svelte +++ b/src/routes/s/[id]/+page.svelte @@ -44,7 +44,8 @@ $: messages = createMessagesList(history, history.currentId); $: canClone = - $sessionUser && ($sessionUser.role === 'admin' || ($sessionUser.permissions?.chat?.import ?? true)); + $sessionUser && + ($sessionUser.role === 'admin' || ($sessionUser.permissions?.chat?.import ?? true)); $: if ($page.params.id) { (async () => { @@ -181,7 +182,7 @@