import { redirect } from '@sveltejs/kit'; import { ApiError, get } from '$lib/server/api'; import { clearToken } from '$lib/server/session'; import type { Captain } from '$lib/types'; import type { LayoutServerLoad } from './$types'; export const load: LayoutServerLoad = async ({ locals, cookies }) => { if (!locals.token) return { captain: null }; try { const captain = await get(locals.token, '/v1/captain'); return { captain: captain.data }; } catch (e) { // Unknown or revoked key: forget it, or every page would fail the same way. // This load runs on every page and before its children, so it catches this first. if (e instanceof ApiError && e.status === 401) { clearToken(cookies); throw redirect(303, '/login?expired=1'); } throw e; } };