import { d as defineEventHandler, $ as getAuthRequestContext, q as readBody, c as createError, dH as isValidPassword, dJ as verifyPasswordResetToken, G as createContentManager, dA as hashPassword, dw as emitAuthPasswordReset } from '../../../nitro/nitro.mjs';
import 'node:crypto';
import 'jsonwebtoken';
import 'node:fs';
import 'node:module';
import 'node:path';
import 'node:url';
import 'node:http';
import 'node:https';
import 'node:events';
import 'node:buffer';
import 'pino';
import 'drizzle-orm/better-sqlite3';
import 'drizzle-orm/node-postgres';
import 'drizzle-orm/mysql2';
import 'better-sqlite3';
import 'pg';
import 'mysql2/promise';
import 'drizzle-orm';
import 'fs';
import 'path';
import 'prom-client';
import 'bcryptjs';
import '@node-rs/argon2';
import 'node:async_hooks';
import 'crypto';
import 'node:dns';
import 'node:util';
import 'node:net';
import 'fs/promises';
import 'url';
import 'semver';

const resetPassword_post = defineEventHandler(async (event) => {
  const reqCtx = getAuthRequestContext(event);
  try {
    const body = await readBody(event);
    if (!body || typeof body !== "object") {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: "Request body must be a valid JSON object"
      });
    }
    const token = typeof body.token === "string" ? body.token : "";
    const password = typeof body.password === "string" ? body.password : "";
    const passwordConfirmation = typeof body.passwordConfirmation === "string" ? body.passwordConfirmation : void 0;
    if (!token || !password) {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: "token and password are required"
      });
    }
    if (passwordConfirmation !== void 0 && password !== passwordConfirmation) {
      throw createError({
        statusCode: 400,
        statusMessage: "Validation Error",
        message: "password and passwordConfirmation must match"
      });
    }
    if (!isValidPassword(password)) {
      throw createError({
        statusCode: 400,
        statusMessage: "Validation Error",
        message: "Password must be at least 6 characters long"
      });
    }
    const tokenPayload = verifyPasswordResetToken(token);
    if (!tokenPayload) {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: "Invalid or expired reset token"
      });
    }
    const userManager = createContentManager("user");
    const existingUser = await userManager.findOne(tokenPayload.id);
    if (!existingUser || String(existingUser.email || "") !== tokenPayload.email) {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: "Invalid or expired reset token"
      });
    }
    const hashedPassword = await hashPassword(password);
    await userManager.update(tokenPayload.id, {
      password: hashedPassword
    });
    await emitAuthPasswordReset(tokenPayload.id, tokenPayload.email, "completed", reqCtx);
    return {
      success: true,
      message: "Password has been reset successfully"
    };
  } catch (error) {
    if (error && typeof error === "object" && "statusCode" in error) {
      throw error;
    }
    if (error instanceof Error && error.message.includes("Schema") && error.message.includes("not found")) {
      throw createError({
        statusCode: 500,
        statusMessage: "Configuration Error",
        message: "User schema is not configured. Please run database sync."
      });
    }
    console.error("[POST /api/auth/reset-password] Error:", error);
    throw createError({
      statusCode: 500,
      statusMessage: "Password Reset Failed",
      message: error instanceof Error ? error.message : "Unknown error occurred"
    });
  }
});

export { resetPassword_post as default };
//# sourceMappingURL=reset-password.post.mjs.map
