Split the server API module into client, guards, form fields and action helpers

This commit is contained in:
2026-09-24 13:59:21 +02:00
parent 155f0e2b6a
commit 351b333529
21 changed files with 99 additions and 97 deletions
+21
View File
@@ -0,0 +1,21 @@
import { redirect } from '@sveltejs/kit';
import type { SessionUser } from '$lib/types';
export function loginRedirect(url: URL): never {
redirect(302, `/auth?returnTo=${encodeURIComponent(url.pathname + url.search)}`);
}
export function requireUser(locals: App.Locals, url: URL): SessionUser {
if (!locals.user) loginRedirect(url);
return locals.user;
}
export function optionalUser(
locals: App.Locals,
url: URL,
allowAnonymousListening: boolean
): SessionUser | null {
if (locals.user) return locals.user;
if (!allowAnonymousListening) loginRedirect(url);
return null;
}