import { nanoid } from 'nanoid';
import { SignJWT } from 'jose';
import { v as validateCredentials } from '../../../chunks/auths_D_cJY4N0.mjs';
import { T as TOKEN } from '../../../chunks/middleware_BbZL2mEI.mjs';
export { renderers } from '../../../renderers.mjs';

const secret = new TextEncoder().encode(undefined                              );
async function POST({ request, cookies }) {
  const formData = await request.formData();
  const email = formData.get("email");
  const password = formData.get("password");
  if (typeof email !== "string" || typeof password !== "string") {
    return new Response(
      JSON.stringify({
        message: "Text Inputs Only"
      }),
      {
        status: 400
      }
    );
  }
  try {
    const user = await validateCredentials({ email, password });
    if (!user) {
      return new Response(
        JSON.stringify({
          message: "Invalid email/password"
        }),
        {
          status: 403
        }
      );
    }
    const token = await new SignJWT({ user: { ...user, password: void 0 } }).setProtectedHeader({ alg: "HS256" }).setJti(nanoid()).setIssuedAt().setExpirationTime("2h").sign(secret);
    cookies.set(TOKEN, token, {
      httpOnly: true,
      path: "/",
      maxAge: 60 * 60 * 2
      // 2 hours in seconds
    });
    return new Response(
      JSON.stringify({
        message: "You're logged in!"
      }),
      {
        status: 200
      }
    );
  } catch (error) {
    return new Response(
      JSON.stringify({
        message: error?.message
      }),
      {
        status: 500
      }
    );
  }
}

const _page = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.defineProperty({
  __proto__: null,
  POST
}, Symbol.toStringTag, { value: 'Module' }));

const page = () => _page;

export { page };
