// signup.js (function () { // تأكيد التحميل if (!window.COGNITO_CONFIG) { console.error("COGNITO_CONFIG missing"); return; } if (typeof AmazonCognitoIdentity === "undefined") { console.error("Cognito SDK missing"); return; } const { CognitoUserPool, CognitoUserAttribute, CognitoUser } = AmazonCognitoIdentity; const form = document.getElementById("signup-form"); if (!form) return; const pool = new CognitoUserPool({ UserPoolId: COGNITO_CONFIG.userPoolId, ClientId: COGNITO_CONFIG.clientId }); function goToConfirm(email) { const isAR = /_ar\.html$/i.test(location.pathname); const page = isAR ? "/reset_confirm_ar.html" : "/reset_confirm.html"; const url = location.origin + page + "?mode=confirm&email=" + encodeURIComponent(email); // (اختياري) تشخيص قبل التحويل: // console.log("DEBUG redirect →", url); // alert("Going to: " + url); location.href = url; } form.addEventListener("submit", function (e) { e.preventDefault(); const name = document.getElementById("fullName").value.trim(); const email = document.getElementById("email").value.trim(); const phone = (document.getElementById("phone")?.value || "").trim(); // اختياري const pass = document.getElementById("password").value; const attrs = [ new CognitoUserAttribute({ Name: "name", Value: name }), new CognitoUserAttribute({ Name: "email", Value: email }) ]; if (phone) attrs.push(new CognitoUserAttribute({ Name: "phone_number", Value: phone })); pool.signUp(email, pass, attrs, null, function (err) { if (err) { if (err.code === "UsernameExistsException") { // مستخدم موجود لكن غير مؤكَّد: أعد إرسال الكود ووجّه للتأكيد const user = new CognitoUser({ Username: email, Pool: pool }); user.resendConfirmationCode(function (e2) { if (e2) alert(e2.message || JSON.stringify(e2)); goToConfirm(email); }); return; } alert(err.message || JSON.stringify(err)); return; } // نجاح التسجيل → توجه لصفحة إدخال الكود goToConfirm(email); }); }); })();