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

export const GET = async (event: APIEvent) => {
  if (!event.nativeEvent.context.session) {
    return new Error("Unauthorized");
  }
  await lucia.invalidateSession(event.nativeEvent.context.session.id);
  appendHeader(
    event,
    "Set-Cookie",
    lucia.createBlankSessionCookie().serialize(),
  );
  return new Response(null, {
    status: httpStatus.FOUND,
    headers: { Location: "/" },
  });
};
