refactor: first load redirection

This commit is contained in:
Arman
2024-03-07 15:59:36 +01:00
parent e5568ddd6c
commit 795c182fb1
2 changed files with 29 additions and 22 deletions
+27 -21
View File
@@ -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<Record<string, string>>;
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);
}
});
+2 -1
View File
@@ -13,7 +13,8 @@ const config = defineConfig({
'@analytics/google-analytics',
'analytics',
'dayjs',
'dotenv'
'dotenv',
'zrender'
]
},
define: {