import { O as fail, N as redirect } from '../../../chunks/utils.js-mIi_c4Oz.js';
import { d as db } from '../../../chunks/db.js-le-jjQPv.js';
import { v as verifyPassword, p as pruneSessions, c as createSession } from '../../../chunks/auth.js-D502gejJ.js';

const actions = {
  default: async ({ request, cookies, url }) => {
    const form = await request.formData();
    const email = String(form.get("email") ?? "").trim().toLowerCase();
    const password = String(form.get("password") ?? "");
    const user = db.prepare("SELECT id, password_hash, active FROM users WHERE email = ?").get(email);
    if (!user || !user.active || !verifyPassword(password, user.password_hash)) {
      return fail(400, { error: "Correo o contraseña incorrectos.", email });
    }
    pruneSessions();
    createSession(user.id, cookies);
    const next = url.searchParams.get("next");
    throw redirect(303, next && next.startsWith("/") ? next : "/");
  }
};

var _page_server_ts = /*#__PURE__*/Object.freeze({
  __proto__: null,
  actions: actions
});

export { _page_server_ts as _ };
//# sourceMappingURL=_page.server.ts.js-Cu15Ety9.js.map
