import { redirect } from "@solidjs/router"; import { getCookie } from "vinxi/http"; import { getSession, SESSION_COOKIE } from "~/lib/session"; import { UnauthorizedError } from "~/lib/errors"; export { RouteError, NotFoundError, ForbiddenError, UnauthorizedError } from "~/lib/errors"; /** * Reads + validates the current session. Throws `UnauthorizedError` so callers * can either let the root `ErrorBoundary` handle it or catch and rethrow a * `redirect(...)` for UI flows. */ export async function requireSession() { "use server"; const sessionId = getCookie(SESSION_COOKIE); if (!sessionId) throw new UnauthorizedError(); const session = await getSession(sessionId); if (!session) throw new UnauthorizedError(); return session; } export async function requireAuth() { "use server"; try { const session = await requireSession(); return session.user; } catch { throw redirect("/"); } } export async function requireNotOwner(targetHandle: string) { "use server"; const user = await requireAuth(); if (user.handle === targetHandle) { throw redirect(`/${targetHandle}`); } return user; } export async function requireOwner(targetHandle: string) { "use server"; const user = await requireAuth(); if (user.handle !== targetHandle) { throw redirect(`/${targetHandle}`); } return user; }