1
0
mirror of https://github.com/pocket-id/pocket-id.git synced 2026-02-12 06:49:00 +00:00

fix: don't load app config and user on every route change

This commit is contained in:
Elias Schneider
2025-06-04 08:52:34 +02:00
parent 14f59ce3f3
commit bdcef60cab
4 changed files with 36 additions and 32 deletions

View File

@@ -0,0 +1,25 @@
import type { User } from '$lib/types/user.type';
// Returns the path to redirect to based on the current path and user authentication status
// If no redirect is needed, it returns null
export function getAuthRedirectPath(path: string, user: User | null) {
const isSignedIn = !!user;
const isAdmin = user?.isAdmin;
const isUnauthenticatedOnlyPath =
path == '/login' || path.startsWith('/login/') || path == '/lc' || path.startsWith('/lc/');
const isPublicPath = ['/authorize', '/device', '/health', '/healthz'].includes(path);
const isAdminPath = path == '/settings/admin' || path.startsWith('/settings/admin/');
if (!isUnauthenticatedOnlyPath && !isPublicPath && !isSignedIn) {
return '/login';
}
if (isUnauthenticatedOnlyPath && isSignedIn) {
return '/settings';
}
if (isAdminPath && !isAdmin) {
return '/settings';
}
}