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:
25
frontend/src/lib/utils/redirection-util.ts
Normal file
25
frontend/src/lib/utils/redirection-util.ts
Normal 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';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user