| @@ -1,6 +1,119 @@ | ||
| 1 | 1 | let currentForm = ""; |
| 2 | +/** Set when the user successfully validates a promo code; sent with signup. */ | |
| 3 | +let validatedPromoCode = null; | |
| 2 | 4 | |
| 5 | +function setPromoFeedback(el, message, type) { | |
| 6 | + if (!el) return; | |
| 7 | + el.textContent = message; | |
| 8 | + el.className = "ai-promo-feedback"; | |
| 9 | + if (type === "success") { | |
| 10 | + el.classList.add("is-success"); | |
| 11 | + } else if (type === "error") { | |
| 12 | + el.classList.add("is-error"); | |
| 13 | + } | |
| 14 | +} | |
| 15 | + | |
| 16 | +function setupPromoForSignupForm(form) { | |
| 17 | + if (!form) return; | |
| 18 | + const toggle = form.querySelector(".ai-promo-toggle-btn"); | |
| 19 | + const panel = form.querySelector(".ai-promo-panel"); | |
| 20 | + const input = form.querySelector(".ai-promo-input"); | |
| 21 | + const validateBtn = form.querySelector(".ai-promo-validate-btn"); | |
| 22 | + const feedback = form.querySelector(".ai-promo-feedback"); | |
| 23 | + if (!toggle || !panel) return; | |
| 24 | + | |
| 25 | + validatedPromoCode = null; | |
| 26 | + if (feedback) { | |
| 27 | + feedback.textContent = ""; | |
| 28 | + feedback.className = "ai-promo-feedback"; | |
| 29 | + } | |
| 30 | + | |
| 31 | + toggle.addEventListener("click", function () { | |
| 32 | + const willShow = panel.hidden; | |
| 33 | + panel.hidden = !willShow; | |
| 34 | + toggle.setAttribute("aria-expanded", willShow ? "true" : "false"); | |
| 35 | + if (willShow && input) { | |
| 36 | + input.focus(); | |
| 37 | + } | |
| 38 | + }); | |
| 39 | + | |
| 40 | + if (input) { | |
| 41 | + input.addEventListener("input", function () { | |
| 42 | + validatedPromoCode = null; | |
| 43 | + if (feedback) { | |
| 44 | + feedback.textContent = ""; | |
| 45 | + feedback.className = "ai-promo-feedback"; | |
| 46 | + } | |
| 47 | + }); | |
| 48 | + } | |
| 49 | + | |
| 50 | + if (validateBtn) { | |
| 51 | + validateBtn.addEventListener("click", async function () { | |
| 52 | + const raw = (input && input.value ? input.value : "").trim(); | |
| 53 | + if (!raw) { | |
| 54 | + setPromoFeedback(feedback, "Please enter a promo code.", "error"); | |
| 55 | + validatedPromoCode = null; | |
| 56 | + return; | |
| 57 | + } | |
| 58 | + validateBtn.disabled = true; | |
| 59 | + validateBtn.classList.add("is-loading"); | |
| 60 | + try { | |
| 61 | + const params = new URLSearchParams({ code: raw }); | |
| 62 | + const res = await fetch( | |
| 63 | + `${window.config.apiUrl}/auth/promo-code/verify?${params.toString()}` | |
| 64 | + ); | |
| 65 | + let data = {}; | |
| 66 | + try { | |
| 67 | + data = await res.json(); | |
| 68 | + } catch (e) { | |
| 69 | + data = {}; | |
| 70 | + } | |
| 71 | + if (!res.ok) { | |
| 72 | + setPromoFeedback( | |
| 73 | + feedback, | |
| 74 | + "Could not verify the promo code. Please try again.", | |
| 75 | + "error" | |
| 76 | + ); | |
| 77 | + validatedPromoCode = null; | |
| 78 | + return; | |
| 79 | + } | |
| 80 | + if (!data.exists) { | |
| 81 | + setPromoFeedback(feedback, "Invalid promo code.", "error"); | |
| 82 | + validatedPromoCode = null; | |
| 83 | + return; | |
| 84 | + } | |
| 85 | + if (!data.valide) { | |
| 86 | + setPromoFeedback( | |
| 87 | + feedback, | |
| 88 | + "This promo code is invalid or has expired.", | |
| 89 | + "error" | |
| 90 | + ); | |
| 91 | + validatedPromoCode = null; | |
| 92 | + return; | |
| 93 | + } | |
| 94 | + validatedPromoCode = raw; | |
| 95 | + setPromoFeedback( | |
| 96 | + feedback, | |
| 97 | + "This promo code is valid and will be applied when you create your account.", | |
| 98 | + "success" | |
| 99 | + ); | |
| 100 | + } catch (err) { | |
| 101 | + console.log(err); | |
| 102 | + setPromoFeedback( | |
| 103 | + feedback, | |
| 104 | + "Could not verify the promo code. Please try again.", | |
| 105 | + "error" | |
| 106 | + ); | |
| 107 | + validatedPromoCode = null; | |
| 108 | + } finally { | |
| 109 | + validateBtn.disabled = false; | |
| 110 | + validateBtn.classList.remove("is-loading"); | |
| 111 | + } | |
| 112 | + }); | |
| 113 | + } | |
| 114 | +} | |
| 115 | + | |
| 3 | 116 | function showMessage(message, type = "info") { |
| 4 | 117 | let container = document.getElementById("message-container"); |
| 5 | 118 | |
| 6 | 119 | // Si le container n'existe pas, le créer |
| @@ -6,8 +119,30 @@ | ||
| 6 | 119 | // Si le container n'existe pas, le créer |
| 7 | 120 | if (!container) { |
| 8 | 121 | container = document.createElement("div"); |
| 9 | 122 | container.id = "message-container"; |
| 123 | + } | |
| 124 | + | |
| 125 | + // Special placement: after successful signup, show the verification notice right under | |
| 126 | + // "Sign in to your account" and above the Email field to make it highly visible. | |
| 127 | + const verificationMsg = "Before signing in, please check your email and click the verification link to activate your account."; | |
| 128 | + const successAndVerifyMsg = "Account created successfully! Before signing in, please check your email and click the verification link to activate your account."; | |
| 129 | + const isVerificationNotice = | |
| 130 | + typeof message === "string" && | |
| 131 | + (message.trim() === verificationMsg || message.trim() === successAndVerifyMsg); | |
| 132 | + | |
| 133 | + if (currentForm === "signin" && isVerificationNotice) { | |
| 134 | + const header = document.querySelector(".ai-auth-header"); | |
| 135 | + const form = document.getElementById("signin-form"); | |
| 136 | + if (header && form && header.parentNode) { | |
| 137 | + // ensure warning style | |
| 138 | + type = "warning"; | |
| 139 | + header.parentNode.insertBefore(container, form); | |
| 140 | + } else { | |
| 141 | + const authContainer = document.querySelector(".ai-auth-container"); | |
| 142 | + if (authContainer) authContainer.appendChild(container); | |
| 143 | + } | |
| 144 | + } else { | |
| 10 | 145 | const authContainer = document.querySelector(".ai-auth-container"); |
| 11 | 146 | if (authContainer) { |
| 12 | 147 | authContainer.appendChild(container); |
| 13 | 148 | } |
| @@ -83,8 +218,9 @@ | ||
| 83 | 218 | document.getElementById("hidden-signin-form-element"); |
| 84 | 219 | |
| 85 | 220 | if (signupForm) { |
| 86 | 221 | signupForm.addEventListener("submit", handleSignup); |
| 222 | + setupPromoForSignupForm(signupForm); | |
| 87 | 223 | |
| 88 | 224 | // Ajouter un listener pour la checkbox des conditions d'utilisation |
| 89 | 225 | const termsCheckbox = signupForm.querySelector("#terms-checkbox"); |
| 90 | 226 | if (termsCheckbox) { |
| @@ -160,13 +296,26 @@ | ||
| 160 | 296 | const siteDomain = (typeof aiBuilderVars !== 'undefined' && aiBuilderVars.siteDomain) |
| 161 | 297 | ? aiBuilderVars.siteDomain |
| 162 | 298 | : window.location.hostname; |
| 163 | 299 | |
| 300 | + const signupBody = { | |
| 301 | + email, | |
| 302 | + password, | |
| 303 | + captcha_token, | |
| 304 | + domain: siteDomain, | |
| 305 | + }; | |
| 306 | + if (typeof aiBuilderVars !== "undefined" && aiBuilderVars.installationId) { | |
| 307 | + signupBody.installationId = aiBuilderVars.installationId; | |
| 308 | + } | |
| 309 | + if (validatedPromoCode) { | |
| 310 | + signupBody.promoCode = validatedPromoCode; | |
| 311 | + } | |
| 312 | + | |
| 164 | 313 | try { |
| 165 | 314 | const response = await fetch(`${window.config.apiUrl}/auth/signup`, { |
| 166 | 315 | method: "POST", |
| 167 | 316 | headers: { "Content-Type": "application/json" }, |
| 168 | - body: JSON.stringify({ email, password, captcha_token, domain: siteDomain }), | |
| 317 | + body: JSON.stringify(signupBody), | |
| 169 | 318 | }); |
| 170 | 319 | |
| 171 | 320 | console.log(response); |
| 172 | 321 | const data = await response.json(); |
| @@ -171,8 +320,9 @@ | ||
| 171 | 320 | console.log(response); |
| 172 | 321 | const data = await response.json(); |
| 173 | 322 | |
| 174 | 323 | if (response.ok) { |
| 324 | + validatedPromoCode = null; | |
| 175 | 325 | // Marquer l'inscription comme réussie |
| 176 | 326 | await fetch(ajaxurl, { |
| 177 | 327 | method: "POST", |
| 178 | 328 | headers: { "Content-Type": "application/x-www-form-urlencoded" }, |
| @@ -179,22 +329,23 @@ | ||
| 179 | 329 | body: |
| 180 | 330 | "action=aibui_set_signup_success&nonce=" + aiBuilderVars.nonce, |
| 181 | 331 | }); |
| 182 | 332 | |
| 333 | + | |
| 334 | + // Basculer vers le formulaire de connexion après 3 secondes | |
| 335 | + toggleForm("signin"); | |
| 183 | 336 | showMessage( |
| 184 | - "Account created successfully! Please check your email to verify your account.", | |
| 185 | - "success" | |
| 337 | + "Account created successfully! Before signing in, please check your email and click the verification link to activate your account.", | |
| 338 | + "warning" | |
| 186 | 339 | ); |
| 187 | - | |
| 188 | - // Basculer vers le formulaire de connexion après 3 secondes | |
| 189 | - setTimeout(() => { | |
| 190 | - toggleForm("signin"); | |
| 191 | - showMessage( | |
| 192 | - "Before signing in, please check your email and click the verification link to activate your account.", | |
| 193 | - "info" | |
| 194 | - ); | |
| 195 | - }, 3000); | |
| 196 | 340 | } else { |
| 341 | + if ( | |
| 342 | + data.message === "account-already-exists-for-website" || | |
| 343 | + data.message === "account-already-exists-for-installation" | |
| 344 | + ) { | |
| 345 | + showMessage("An account already exists on this WordPress site. Please sign in instead.", "error"); | |
| 346 | + return; | |
| 347 | + } | |
| 197 | 348 | showMessage(data.message || "Signup failed. Please try again.", "error"); |
| 198 | 349 | } |
| 199 | 350 | } catch (error) { |
| 200 | 351 | console.log(error); |
| @@ -439,8 +590,18 @@ | ||
| 439 | 590 | |
| 440 | 591 | // Fonction pour afficher les informations utilisateur |
| 441 | 592 | function displayUserInfo(userData) { |
| 442 | 593 | console.log("🔍 User data:", userData); |
| 594 | + const accountEmail = document.getElementById("account-email"); | |
| 595 | + if (accountEmail) { | |
| 596 | + const email = | |
| 597 | + userData?.email || | |
| 598 | + userData?.userEmail || | |
| 599 | + userData?.mail || | |
| 600 | + ""; | |
| 601 | + accountEmail.textContent = email || "-"; | |
| 602 | + } | |
| 603 | + | |
| 443 | 604 | // Afficher le plan |
| 444 | 605 | const planBadge = document.getElementById("plan-badge"); |
| 445 | 606 | if (planBadge) { |
| 446 | 607 | const plan = userData.plan || "basic"; |