| 1 |
function showMessage(message, type = "info") { |
| 2 |
let container = document.getElementById("message-container"); |
| 3 |
|
| 4 |
if (!container) { |
| 5 |
container = document.createElement("div"); |
| 6 |
container.id = "message-container"; |
| 7 |
const resetContainer = document.querySelector(".ai-reset-container"); |
| 8 |
if (resetContainer) { |
| 9 |
resetContainer.appendChild(container); |
| 10 |
} |
| 11 |
} |
| 12 |
|
| 13 |
container.innerHTML = `<div class="ai-message ${type}">${message}</div>`; |
| 14 |
container.scrollIntoView({ behavior: "smooth" }); |
| 15 |
} |
| 16 |
|
| 17 |
function setLoading(buttonId, isLoading) { |
| 18 |
const button = document.getElementById(buttonId); |
| 19 |
|
| 20 |
if (!button) { |
| 21 |
console.error(`Button with id '${buttonId}' not found`); |
| 22 |
return; |
| 23 |
} |
| 24 |
|
| 25 |
let loading = button.querySelector(".ai-loading"); |
| 26 |
|
| 27 |
if (!loading) { |
| 28 |
loading = document.createElement("span"); |
| 29 |
loading.className = "ai-loading"; |
| 30 |
loading.style.display = "none"; |
| 31 |
button.appendChild(loading); |
| 32 |
} |
| 33 |
|
| 34 |
if (!button.dataset.originalText) { |
| 35 |
button.dataset.originalText = button.textContent.trim(); |
| 36 |
} |
| 37 |
|
| 38 |
if (isLoading) { |
| 39 |
loading.style.display = "inline-block"; |
| 40 |
button.disabled = true; |
| 41 |
button.textContent = ""; |
| 42 |
button.appendChild(loading); |
| 43 |
button.appendChild(document.createTextNode(button.dataset.originalText)); |
| 44 |
} else { |
| 45 |
loading.style.display = "none"; |
| 46 |
button.disabled = false; |
| 47 |
button.textContent = button.dataset.originalText; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
async function handleEmailSubmit(e) { |
| 52 |
e.preventDefault(); |
| 53 |
const form = e.target; |
| 54 |
const email = form.email.value; |
| 55 |
const nonce = form.querySelector('input[name="aibui_nonce"]').value; |
| 56 |
|
| 57 |
// Vérifier le nonce côté client (validation supplémentaire côté serveur) |
| 58 |
if (!nonce) { |
| 59 |
showMessage( |
| 60 |
"Security check failed. Please refresh the page and try again.", |
| 61 |
"error" |
| 62 |
); |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
setLoading("send-reset-btn", true); |
| 67 |
|
| 68 |
try { |
| 69 |
const response = await fetch( |
| 70 |
window.config.apiUrl + "/user/request-password-reset", |
| 71 |
{ |
| 72 |
method: "POST", |
| 73 |
headers: { "Content-Type": "application/json" }, |
| 74 |
body: JSON.stringify({ email, nonce }), |
| 75 |
} |
| 76 |
); |
| 77 |
|
| 78 |
console.log("response", response); |
| 79 |
|
| 80 |
const status = await response.status; |
| 81 |
|
| 82 |
if (status === 200) { |
| 83 |
showMessage( |
| 84 |
"Password reset link sent to your email address. Please check your inbox.", |
| 85 |
"success" |
| 86 |
); |
| 87 |
form.reset(); |
| 88 |
} else { |
| 89 |
const data = await response.json(); |
| 90 |
showMessage( |
| 91 |
data.message || "Failed to send reset link. Please try again.", |
| 92 |
"error" |
| 93 |
); |
| 94 |
} |
| 95 |
} catch (error) { |
| 96 |
console.error("Reset email error:", error); |
| 97 |
showMessage("Network error. Please check your connection.", "error"); |
| 98 |
} finally { |
| 99 |
setLoading("send-reset-btn", false); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
async function handlePasswordReset(e) { |
| 104 |
e.preventDefault(); |
| 105 |
const form = e.target; |
| 106 |
const token = form.token.value; |
| 107 |
const email = form.email.value; |
| 108 |
const password = form.password.value; |
| 109 |
const confirmPassword = form.confirm_password.value; |
| 110 |
const nonce = form.querySelector('input[name="aibui_nonce"]').value; |
| 111 |
|
| 112 |
// Vérifier le nonce côté client (validation supplémentaire côté serveur) |
| 113 |
if (!nonce) { |
| 114 |
showMessage( |
| 115 |
"Security check failed. Please refresh the page and try again.", |
| 116 |
"error" |
| 117 |
); |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
if (password !== confirmPassword) { |
| 122 |
showMessage("Passwords do not match", "error"); |
| 123 |
return; |
| 124 |
} |
| 125 |
|
| 126 |
if (password.length < 8) { |
| 127 |
showMessage("Password must be at least 8 characters long", "error"); |
| 128 |
return; |
| 129 |
} |
| 130 |
|
| 131 |
setLoading("reset-password-btn", true); |
| 132 |
|
| 133 |
try { |
| 134 |
const response = await fetch( |
| 135 |
window.config.apiUrl + "/auth/reset-password", |
| 136 |
{ |
| 137 |
method: "POST", |
| 138 |
headers: { "Content-Type": "application/json" }, |
| 139 |
body: JSON.stringify({ token, email, password, nonce }), |
| 140 |
} |
| 141 |
); |
| 142 |
|
| 143 |
const data = await response.json(); |
| 144 |
|
| 145 |
if (response.ok) { |
| 146 |
showMessage( |
| 147 |
"Password reset successfully! Redirecting to sign in...", |
| 148 |
"success" |
| 149 |
); |
| 150 |
setTimeout(() => { |
| 151 |
window.location.href = aiBuilderVars.accountUrl; |
| 152 |
}, 2000); |
| 153 |
} else { |
| 154 |
showMessage( |
| 155 |
data.message || "Password reset failed. Please try again.", |
| 156 |
"error" |
| 157 |
); |
| 158 |
} |
| 159 |
} catch (error) { |
| 160 |
console.error("Password reset error:", error); |
| 161 |
showMessage("Network error. Please check your connection.", "error"); |
| 162 |
} finally { |
| 163 |
setLoading("reset-password-btn", false); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
document.addEventListener("DOMContentLoaded", function () { |
| 168 |
const emailForm = document.getElementById("email-form"); |
| 169 |
const passwordForm = document.getElementById("password-form"); |
| 170 |
|
| 171 |
if (emailForm) { |
| 172 |
emailForm.addEventListener("submit", handleEmailSubmit); |
| 173 |
} |
| 174 |
|
| 175 |
if (passwordForm) { |
| 176 |
passwordForm.addEventListener("submit", handlePasswordReset); |
| 177 |
} |
| 178 |
}); |
| 179 |
|