Menu
Public repository / 66e3647948f2e277afdd6ecd16939de8b6ad525f
AkurAI-Mail / src/hooks.server.ts
Latest change 66e3647948f2e277afdd6ecd16939de8b6ad525f - Initial commit: AkurAI Mail UI (SvelteKit+Bun) by Ólafur Búi Ólafsson
import { building } from "$app/environment";
import { redirect, type Handle } from "@sveltejs/kit";
import { isAuthenticated, isMailboxAuthenticated } from "$lib/auth";
const publicPrefixes = ["/login", "/webmail/login", "/logout", "/_app", "/favicon", "/robots.txt"];
export const handle: Handle = async ({ event, resolve }) => {
if (building) return resolve(event);
const path = event.url.pathname;
const isPublic = publicPrefixes.some((prefix) => path.startsWith(prefix));
if (isPublic || isAuthenticated(event.cookies)) return resolve(event);
if (path.startsWith("/webmail") && isMailboxAuthenticated(event.cookies)) return resolve(event);
if (path.startsWith("/api/") || path.startsWith("/webmail/api")) {
return new Response(JSON.stringify({ ok: false, error: "authentication required" }), {
status: 401,
headers: { "content-type": "application/json" }
});
}
if (path.startsWith("/webmail")) {
throw redirect(303, `/webmail/login?next=${encodeURIComponent(path + event.url.search)}`);
}
throw redirect(303, `/login?next=${encodeURIComponent(path)}`);
};