Files
webref/frontend/src/hooks.server.ts
Danilo Reyes 010df31455 phase 5
2025-11-02 11:07:42 -06:00

35 lines
943 B
TypeScript

/**
* SvelteKit server hooks for route protection
*/
import type { Handle } from '@sveltejs/kit';
// Protected routes that require authentication
const protectedRoutes = ['/boards', '/library', '/settings'];
export const handle: Handle = async ({ event, resolve }) => {
const { url, cookies } = event;
const pathname = url.pathname;
// Check if route requires authentication
const requiresAuth = protectedRoutes.some((route) => pathname.startsWith(route));
if (requiresAuth) {
// Check for auth token in cookies (or you could check localStorage via client-side)
const authToken = cookies.get('auth_token');
if (!authToken) {
// Redirect to login if not authenticated
return new Response(null, {
status: 302,
headers: {
location: `/login?redirect=${encodeURIComponent(pathname)}`,
},
});
}
}
const response = await resolve(event);
return response;
};