Merge pull request #937 from appwrite/refactor-first-load-redirection

refactor: first load redirection
This commit is contained in:
Torsten Dittmann
2024-03-10 17:54:55 +01:00
committed by GitHub
+27 -22
View File
@@ -6,7 +6,6 @@
import { isTrackingAllowed, trackPageView } from '$lib/actions/analytics';
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';
@@ -15,6 +14,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';
onMount(async () => {
if ($page.url.searchParams.has('migrate')) {
@@ -47,38 +47,43 @@
/**
* Handle initial load.
*/
if (!$page.url.pathname.startsWith('/auth') && !$page.url.pathname.startsWith('/git')) {
const acceptedRoutes = [
'/login',
'/register',
'/recover',
'/invite',
'/card',
'/hackathon',
'/mfa'
];
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',
'/mfa'
];
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);
}
});