# ai-builder/2.7.10/assets/js/reset-password.js

AI Builder – Generate pages, blocks, images &amp; translate with AI, version 2.7.10. 179 lines.

- Page: https://pluginprobe.com/plugins/ai-builder/2.7.10/code/assets/js/reset-password.js
- Raw: https://pluginprobe.com/plugins/ai-builder/2.7.10/raw/assets/js/reset-password.js
- Modified: 2025-08-23T08:19:58+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/ai-builder/2.7.10/code/assets/js/reset-password.js#L10-L20`.

```javascript
function showMessage(message, type = "info") {
  let container = document.getElementById("message-container");

  if (!container) {
    container = document.createElement("div");
    container.id = "message-container";
    const resetContainer = document.querySelector(".ai-reset-container");
    if (resetContainer) {
      resetContainer.appendChild(container);
    }
  }

  container.innerHTML = `<div class="ai-message ${type}">${message}</div>`;
  container.scrollIntoView({ behavior: "smooth" });
}

function setLoading(buttonId, isLoading) {
  const button = document.getElementById(buttonId);

  if (!button) {
    console.error(`Button with id '${buttonId}' not found`);
    return;
  }

  let loading = button.querySelector(".ai-loading");

  if (!loading) {
    loading = document.createElement("span");
    loading.className = "ai-loading";
    loading.style.display = "none";
    button.appendChild(loading);
  }

  if (!button.dataset.originalText) {
    button.dataset.originalText = button.textContent.trim();
  }

  if (isLoading) {
    loading.style.display = "inline-block";
    button.disabled = true;
    button.textContent = "";
    button.appendChild(loading);
    button.appendChild(document.createTextNode(button.dataset.originalText));
  } else {
    loading.style.display = "none";
    button.disabled = false;
    button.textContent = button.dataset.originalText;
  }
}

async function handleEmailSubmit(e) {
  e.preventDefault();
  const form = e.target;
  const email = form.email.value;
  const nonce = form.querySelector('input[name="aibui_nonce"]').value;

  // Vérifier le nonce côté client (validation supplémentaire côté serveur)
  if (!nonce) {
    showMessage(
      "Security check failed. Please refresh the page and try again.",
      "error"
    );
    return;
  }

  setLoading("send-reset-btn", true);

  try {
    const response = await fetch(
      window.config.apiUrl + "/user/request-password-reset",
      {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ email, nonce }),
      }
    );

    console.log("response", response);

    const status = await response.status;

    if (status === 200) {
      showMessage(
        "Password reset link sent to your email address. Please check your inbox.",
        "success"
      );
      form.reset();
    } else {
      const data = await response.json();
      showMessage(
        data.message || "Failed to send reset link. Please try again.",
        "error"
      );
    }
  } catch (error) {
    console.error("Reset email error:", error);
    showMessage("Network error. Please check your connection.", "error");
  } finally {
    setLoading("send-reset-btn", false);
  }
}

async function handlePasswordReset(e) {
  e.preventDefault();
  const form = e.target;
  const token = form.token.value;
  const email = form.email.value;
  const password = form.password.value;
  const confirmPassword = form.confirm_password.value;
  const nonce = form.querySelector('input[name="aibui_nonce"]').value;

  // Vérifier le nonce côté client (validation supplémentaire côté serveur)
  if (!nonce) {
    showMessage(
      "Security check failed. Please refresh the page and try again.",
      "error"
    );
    return;
  }

  if (password !== confirmPassword) {
    showMessage("Passwords do not match", "error");
    return;
  }

  if (password.length < 8) {
    showMessage("Password must be at least 8 characters long", "error");
    return;
  }

  setLoading("reset-password-btn", true);

  try {
    const response = await fetch(
      window.config.apiUrl + "/auth/reset-password",
      {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ token, email, password, nonce }),
      }
    );

    const data = await response.json();

    if (response.ok) {
      showMessage(
        "Password reset successfully! Redirecting to sign in...",
        "success"
      );
      setTimeout(() => {
        window.location.href = aiBuilderVars.accountUrl;
      }, 2000);
    } else {
      showMessage(
        data.message || "Password reset failed. Please try again.",
        "error"
      );
    }
  } catch (error) {
    console.error("Password reset error:", error);
    showMessage("Network error. Please check your connection.", "error");
  } finally {
    setLoading("reset-password-btn", false);
  }
}

document.addEventListener("DOMContentLoaded", function () {
  const emailForm = document.getElementById("email-form");
  const passwordForm = document.getElementById("password-form");

  if (emailForm) {
    emailForm.addEventListener("submit", handleEmailSubmit);
  }

  if (passwordForm) {
    passwordForm.addEventListener("submit", handlePasswordReset);
  }
});

```
