import { APIEvent } from "@solidjs/start/server";
import httpStatus from "http-status";
import { appendHeader } from "vinxi/http";
import { lucia } from "~/lib/auth";

export const GET = async (event: APIEvent) => {
  const { session } = event.nativeEvent.context;

  if (!session) return new Error("Unauthorized");

  await lucia.invalidateSession(session.id);
  appendHeader("Set-Cookie", lucia.createBlankSessionCookie().serialize());
  return new Response(null, {
    status: httpStatus.FOUND,
    headers: { Location: "/" },
  });
};
