diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 49e6ea616..1452936b0 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -7,7 +7,6 @@ import { reportWebVitals } from '$lib/helpers/vitals'; import { Notifications, Progress } from '$lib/layout'; import { app } from '$lib/stores/app'; - import { user } from '$lib/stores/user'; import { ENV, isCloud } from '$lib/system'; import * as Sentry from '@sentry/svelte'; import LogRocket from 'logrocket'; @@ -17,6 +16,7 @@ import { loading, requestedMigration } from './store'; import { parseIfString } from '$lib/helpers/object'; import Consent, { consent } from '$lib/components/consent.svelte'; + import type { Models } from '@appwrite.io/console'; if (browser) { window.VERCEL_ANALYTICS_ID = import.meta.env.VERCEL_ANALYTICS_ID?.toString() ?? false; @@ -64,37 +64,43 @@ /** * Handle initial load. */ - if (!$page.url.pathname.startsWith('/auth') && !$page.url.pathname.startsWith('/git')) { - const acceptedRoutes = [ - '/login', - '/register', - '/recover', - '/invite', - '/card', - '/hackathon' - ]; - if ($user) { - if ( - !$page.url.pathname.startsWith('/console') && - !$page.url.pathname.startsWith('/invite') && - !$page.url.pathname.startsWith('/card') && - !$page.url.pathname.startsWith('/hackathon') - ) { + + function shouldRedirect(route: string, routes: string[]) { + return !routes.some((n) => route.startsWith(n)); + } + + const authenticationRoutes = ['/auth', '/git']; + const acceptedUnauthenticatedRoutes = [ + '/login', + '/register', + '/recover', + '/invite', + '/card', + '/hackathon', + '/test' + ]; + const acceptedAuthenticatedRoutes = ['/console', '/invite', '/card', '/hackathon']; + + const pathname = $page.url.pathname; + const user = $page.data.account as Models.User>; + + if (shouldRedirect(pathname, authenticationRoutes)) { + if (user?.$id) { + if (shouldRedirect(pathname, acceptedAuthenticatedRoutes)) { await goto(`${base}/console`, { replaceState: true }); } - loading.set(false); } else { - if (acceptedRoutes.some((n) => $page.url.pathname.startsWith(n))) { - await goto(`${base}${$page.url.pathname}${$page.url.search}`); + if (acceptedUnauthenticatedRoutes.some((n) => pathname.startsWith(n))) { + await goto(`${base}${pathname}${$page.url.search}`); } else { await goto(`${base}/login`, { replaceState: true }); } - loading.set(false); } + loading.set(false); } }); diff --git a/vite.config.ts b/vite.config.ts index 754fb4acc..d1d53436e 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -13,7 +13,8 @@ const config = defineConfig({ '@analytics/google-analytics', 'analytics', 'dayjs', - 'dotenv' + 'dotenv', + 'zrender' ] }, define: {