import { Actions, redirect } from "@sveltejs/kit";
import { optima } from "$lib";

export const actions: Actions = {
  login: async (event) => {
    const data = await event.request.formData();

    const tokens = await optima.user.awaitAuthCallback(
      data.get("callbackKey") as string,
    );

    event.cookies.set("accessToken", tokens.accessToken, {
      httpOnly: true,
      path: "/",
    });
    event.cookies.set("refreshToken", tokens.refreshToken, {
      httpOnly: true,
      path: "/",
    });

    // Redirect to home page after successful login
    redirect(303, "/");
  },
} satisfies Actions;
