refac
This commit is contained in:
1 parent
1f0dc90abe
commit
0e0d08382a
3 files changed
+19
-23
No files matched your search
@@ -227,9 +227,14 @@
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const gotoAuth = async () => {
|
||||||
|
const currentUrl = `${$page.url.pathname}${$page.url.search}`;
|
||||||
|
await goto(`/auth?redirect=${encodeURIComponent(currentUrl)}`);
|
||||||
|
};
|
||||||
|
|
||||||
onMount(async () => {
|
onMount(async () => {
|
||||||
if ($user === undefined || $user === null) {
|
if ($user === undefined || $user === null) {
|
||||||
await goto('/auth');
|
await gotoAuth();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!['user', 'admin'].includes($user?.role)) {
|
if (!['user', 'admin'].includes($user?.role)) {
|
||||||
@@ -392,6 +397,10 @@
|
|||||||
void openSettingsFromUrl();
|
void openSettingsFromUrl();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$: if (loaded && ($user === undefined || $user === null)) {
|
||||||
|
void gotoAuth();
|
||||||
|
}
|
||||||
|
|
||||||
const checkForVersionUpdates = async () => {
|
const checkForVersionUpdates = async () => {
|
||||||
version = await getVersionUpdates(localStorage.token).catch((error) => {
|
version = await getVersionUpdates(localStorage.token).catch((error) => {
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -837,8 +837,8 @@
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const redirectToAuthAfterUnauthorized = () => {
|
const clearExpiredSession = () => {
|
||||||
if (isAuthRedirectInProgress || window.location.pathname === '/auth') {
|
if (isAuthRedirectInProgress) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -855,11 +855,7 @@
|
|||||||
console.error('Error signing out expired session:', error);
|
console.error('Error signing out expired session:', error);
|
||||||
});
|
});
|
||||||
toast.error($i18n.t('Session expired. Please sign in again.'));
|
toast.error($i18n.t('Session expired. Please sign in again.'));
|
||||||
|
isAuthRedirectInProgress = false;
|
||||||
const currentPath = `${window.location.pathname}${window.location.search}`;
|
|
||||||
goto(`/auth?redirect=${encodeURIComponent(currentPath)}`).finally(() => {
|
|
||||||
isAuthRedirectInProgress = false;
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const isCurrentSessionUnauthorized = async (originalFetch) => {
|
const isCurrentSessionUnauthorized = async (originalFetch) => {
|
||||||
@@ -885,7 +881,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (now >= exp - TOKEN_EXPIRY_BUFFER) {
|
if (now >= exp - TOKEN_EXPIRY_BUFFER) {
|
||||||
redirectToAuthAfterUnauthorized();
|
clearExpiredSession();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -1003,7 +999,7 @@
|
|||||||
isAuthenticatedBackendFetch(input, init) &&
|
isAuthenticatedBackendFetch(input, init) &&
|
||||||
(await isCurrentSessionUnauthorized(originalFetch))
|
(await isCurrentSessionUnauthorized(originalFetch))
|
||||||
) {
|
) {
|
||||||
redirectToAuthAfterUnauthorized();
|
clearExpiredSession();
|
||||||
}
|
}
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
@@ -1189,9 +1185,6 @@
|
|||||||
if ($config) {
|
if ($config) {
|
||||||
await setupSocket($config.features?.enable_websocket ?? true);
|
await setupSocket($config.features?.enable_websocket ?? true);
|
||||||
|
|
||||||
const currentUrl = `${window.location.pathname}${window.location.search}`;
|
|
||||||
const encodedUrl = encodeURIComponent(currentUrl);
|
|
||||||
|
|
||||||
if (localStorage.token) {
|
if (localStorage.token) {
|
||||||
// Get Session User Info
|
// Get Session User Info
|
||||||
const sessionUser = await getSessionUser(localStorage.token).catch((error) => {
|
const sessionUser = await getSessionUser(localStorage.token).catch((error) => {
|
||||||
@@ -1223,15 +1216,8 @@
|
|||||||
.catch(() => {});
|
.catch(() => {});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Redirect Invalid Session User to /auth Page
|
|
||||||
localStorage.removeItem('token');
|
localStorage.removeItem('token');
|
||||||
await goto(`/auth?redirect=${encodedUrl}`);
|
await user.set(null);
|
||||||
}
|
|
||||||
} 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}`);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,7 +44,8 @@
|
|||||||
|
|
||||||
$: messages = createMessagesList(history, history.currentId);
|
$: messages = createMessagesList(history, history.currentId);
|
||||||
$: canClone =
|
$: canClone =
|
||||||
$sessionUser && ($sessionUser.role === 'admin' || ($sessionUser.permissions?.chat?.import ?? true));
|
$sessionUser &&
|
||||||
|
($sessionUser.role === 'admin' || ($sessionUser.permissions?.chat?.import ?? true));
|
||||||
|
|
||||||
$: if ($page.params.id) {
|
$: if ($page.params.id) {
|
||||||
(async () => {
|
(async () => {
|
||||||
@@ -181,7 +182,7 @@
|
|||||||
<div
|
<div
|
||||||
class="pt-5 px-2 w-full {($settings?.widescreenMode ?? null)
|
class="pt-5 px-2 w-full {($settings?.widescreenMode ?? null)
|
||||||
? 'max-w-full'
|
? 'max-w-full'
|
||||||
: 'max-w-[52rem]'} mx-auto"
|
: 'max-w-[58rem]'} mx-auto"
|
||||||
>
|
>
|
||||||
<div class="px-3">
|
<div class="px-3">
|
||||||
<h1 class=" text-2xl font-normal line-clamp-1 m-0">
|
<h1 class=" text-2xl font-normal line-clamp-1 m-0">
|
||||||
|
|||||||
Reference in new issue
Block a user