import { redirect, type Handle } from '@sveltejs/kit';

import { getAccountFromCookies } from '$lib/server/auth';

export const handle: Handle = async ({ event, resolve }) => {
  event.locals.account = getAccountFromCookies(event.cookies);

  const path = event.url.pathname;

  // /dashboard/* requires a session. Bounce to the entry page if missing.
  if (path.startsWith('/dashboard') && !event.locals.account) {
    throw redirect(303, '/');
  }

  return resolve(event);
};
