import { redirect } from '@sveltejs/kit'; import { building } from '$app/environment'; const API_BASE = building ? 'http://localhost:8080/api' : '/api'; const PUBLIC_PREFIXES = ['/_app', '/api']; export async function handle({ event, resolve }) { const isStatic = PUBLIC_PREFIXES.some((p) => event.url.pathname.startsWith(p)); const isLogin = event.url.pathname === '/login'; const isPublic = event.url.pathname === '/' || event.url.pathname.startsWith('/status/'); if (isStatic || isLogin || isPublic) { return resolve(event); } if (event.url.pathname.startsWith('/admin')) { const token = event.cookies.get('auth_token'); if (!token) { throw redirect(302, '/login'); } const res = await fetch(`${API_BASE}/auth/verify`, { headers: { Authorization: `Bearer ${token}` } }); if (!res.ok) { throw redirect(302, '/login'); } event.locals.token = token; } return resolve(event); }