This commit is contained in:
Jakub Dorfman 2026-05-18 17:18:09 +02:00
commit b4fbf3e411
59 changed files with 4735 additions and 0 deletions

28
app/src/hooks.server.ts Normal file
View file

@ -0,0 +1,28 @@
import { redirect, type Handle } from '@sveltejs/kit';
import { env } from '$env/dynamic/private';
import { createPocketBase } from '$lib/pb';
export const handle: Handle = async ({ event, resolve }) => {
event.locals.pb = createPocketBase(
event.request.headers.get('cookie') ?? '',
env.POCKETBASE_INTERNAL_URL || env.PUBLIC_POCKETBASE_URL || 'http://localhost:8090'
);
event.locals.user = event.locals.pb.authStore.record;
const path = event.url.pathname;
const isAdminRoute = path.startsWith('/admin');
const isLoginRoute = path === '/admin/login';
const isAuthed = event.locals.pb.authStore.isValid;
if (isAdminRoute && !isLoginRoute && !isAuthed) {
throw redirect(303, '/admin/login');
}
if (isLoginRoute && isAuthed) {
throw redirect(303, '/admin/pages');
}
const response = await resolve(event);
response.headers.append('set-cookie', event.locals.pb.authStore.exportToCookie({ sameSite: 'lax' }, 'pb_auth'));
return response;
};