import { q as query } from '../../../chunks/db_Du3AmLCY.mjs';
import { g as generateToken } from '../../../chunks/user-auth_D4NBmYxs.mjs';
import { s as sendMagicLinkEmail } from '../../../chunks/email_DspzjvY3.mjs';
export { renderers } from '../../../renderers.mjs';

const POST = async ({ request }) => {
  try {
    const { email } = await request.json();
    if (!email || !email.includes("@")) {
      return new Response(JSON.stringify({ error: "Ongeldig emailadres" }), { status: 400 });
    }
    const result = await query("SELECT id, email, email_verified FROM users WHERE email = $1", [email.toLowerCase().trim()]);
    if (!result.rows.length) {
      return new Response(JSON.stringify({ ok: true, message: "Als dit emailadres bij ons bekend is, ontvang je een inloglink." }));
    }
    const user = result.rows[0];
    const token = generateToken();
    const expiresAt = new Date(Date.now() + 36e5);
    await query("UPDATE users SET magic_link_token = $1, magic_link_expires_at = $2 WHERE id = $3", [token, expiresAt, user.id]);
    await sendMagicLinkEmail(email, token);
    return new Response(JSON.stringify({ ok: true, message: "Check je email voor de inloglink!" }));
  } catch (err) {
    console.error("Login error:", err);
    return new Response(JSON.stringify({ error: "Er ging iets mis" }), { status: 500 });
  }
};

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

const page = () => _page;

export { page };
