const sections = [ "loginEmailSection", "loginEmailOtpSection", "loginNameSection", "loginPhoneSection", "loginPhoneOtpSection", ]; let currentSectionIndex = 0; let timer = undefined; if ( localStorage.getItem("loginMessageRedirect") && localStorage.getItem("loginMessageRedirect") != "" ) { setMessage( "login__redirect_from_signup", localStorage.getItem("loginMessageRedirect") ); localStorage.removeItem("loginMessageRedirect"); } else { localStorage.removeItem("loginMessageRedirect"); } const onLoginEmailSubmit = (event) => { event.preventDefault(); const emailElem = document.querySelector("#loginEmailInput"); const enteredEmail = emailElem?.value || " your email"; if (!emailValidation(enteredEmail)) { emailElem.style.outline = "2px solid #f34213"; } else if ( !passwordValidation(document.querySelector("#loginPasswordInput").value) ) { document.querySelector("#loginPasswordInput").style.outline = "2px solid #f34213"; } else { const emailPlaceholder = document.querySelector("#loginUserEmail"); emailPlaceholder.innerHTML = enteredEmail; const myHeaders = new Headers(); myHeaders.append("Content-Type", "application/x-www-form-urlencoded"); const urlencoded = new URLSearchParams(); urlencoded.append("email", enteredEmail); urlencoded.append( "password", document.querySelector("#loginPasswordInput")?.value || "" ); const requestOptions = { method: "POST", headers: myHeaders, body: urlencoded, redirect: "follow", }; fetch(`${getApiLink()}/api/login/login_verify.php`, requestOptions) .then((response) => response.json()) .then((result) => { if (result.success) { emailElem.value = ""; document.querySelector("#loginPasswordInput").value = ""; localStorage.setItem("token", result.data.token); switchSection(1); timer = setTimer("login__email_otp_countdown", "login__resend_email_otp"); } else { createAlertMessage("login__alert_container", result); } }) .catch((error) => console.error(error)); } }; const onLoginEmailOtpSubmit = (event) => { event.preventDefault(); const otpElem = document.querySelector("#loginEmailOtpInput"); if (otpElem.value.length != 6) return; const myHeaders = new Headers(); myHeaders.append("Content-Type", "application/x-www-form-urlencoded"); myHeaders.append("Authorization", localStorage.getItem("token")); const urlencoded = new URLSearchParams(); urlencoded.append("otp", otpElem.value.trim()); const requestOptions = { method: "POST", headers: myHeaders, body: urlencoded, redirect: "follow", }; fetch(`${getApiLink()}/api/login/login_validate.php`, requestOptions) .then((response) => response.json()) .then((result) => { if (result.success) { otpElem.value = ""; localStorage.setItem("token", result?.data?.token || ""); if (result.status == "name_not_set") { switchSection(2); } else if (result.status == "phone_not_set") { switchSection(3); } else { redirectToPage(result?.data?.token || ""); } } else { createAlertMessage("login__alert_container", result); loginOtpError = true; } }) .catch((error) => console.error(error)); }; const onLoginNameSubmit = (event) => { event.preventDefault(); const fnameInput = document.querySelector("#loginFirstNameInput"); const lnameInput = document.querySelector("#loginLastNameInput"); if (!(fnameInput.value.trim().length > 0)) { fnameInput.style.outline = "2px solid #f34213"; } else if (!(lnameInput.value.trim().length > 0)) { lnameInput.style.outline = "2px solid #f34213"; } else { const fname = fnameInput.value.trim(); const lname = lnameInput.value.trim(); const myHeaders = new Headers(); myHeaders.append("Content-Type", "application/x-www-form-urlencoded"); myHeaders.append("Authorization", localStorage.getItem("token")); const urlencoded = new URLSearchParams(); urlencoded.append("fname", fname); urlencoded.append("lname", lname); const requestOptions = { method: "POST", headers: myHeaders, body: urlencoded, redirect: "follow", }; fetch(`${getApiLink()}/api/login/login_add_name.php`, requestOptions) .then((response) => response.json()) .then((result) => { if (result.success) { fnameInput.value = ""; lnameInput.value = ""; switchSection(3); } else { createAlertMessage("login__alert_container", result); } }) .catch((error) => console.error(error)); } }; document.addEventListener("DOMContentLoaded", function () { localStorage.removeItem("token"); initPhoneInput() }); const onResendEmailOtp = () => { const myHeaders = new Headers(); myHeaders.append("Content-Type", "application/x-www-form-urlencoded"); myHeaders.append("Authorization", localStorage.getItem("token")); const requestOptions = { method: "POST", headers: myHeaders, redirect: "follow", }; fetch(`${getApiLink()}/api/services/resend_email_otp.php`, requestOptions) .then((response) => response.json()) .then((result) => { if (result.success) { timer = setTimer("login__email_otp_countdown", "login__resend_email_otp"); } else { createAlertMessage("login__alert_container", result); } }) .catch((error) => console.error(error)); }; const onLoginPhoneSubmit = (event) => { event.preventDefault(); const phoneInput = document.querySelector("#loginPhoneInput"); if (numberValidation(phoneInput.value).valid) { const phoneInput = document.querySelector("#loginPhoneInput"); enteredPhone = phoneInput.value.trim().replaceAll(" ", ""); const myHeaders = new Headers(); myHeaders.append("Content-Type", "application/x-www-form-urlencoded"); myHeaders.append("Authorization", localStorage.getItem("token")); const urlencoded = new URLSearchParams(); urlencoded.append("phone", enteredPhone); const requestOptions = { method: "POST", headers: myHeaders, body: urlencoded, redirect: "follow", }; fetch(`${getApiLink()}/api/login/login_add_phone.php`, requestOptions) .then((response) => response.json()) .then((result) => { if (result.success) { if (result.data?.is_phone_verification_needed) { phoneInput.value = ""; const emailPlaceholder = document.querySelector("#loginUserPhone"); emailPlaceholder.innerHTML = result?.data?.phone || enteredPhone; switchSection(4); timer = setTimer("login__phone_otp_countdown", "login__resend_phone_otp"); } else { localStorage.setItem("token", result?.data?.token || ""); redirectToPage(result?.data?.token || ""); } } else { createAlertMessage("login__alert_container", result); } }) .catch((error) => console.error(error)); } else { phoneInput.style.outline = "2px solid #f34213"; } }; const onResendPhoneOtp = () => { const myHeaders = new Headers(); myHeaders.append("Content-Type", "application/x-www-form-urlencoded"); myHeaders.append("Authorization", localStorage.getItem("token")); const requestOptions = { method: "POST", headers: myHeaders, redirect: "follow", }; fetch(`${getApiLink()}/api/services/resend_phone_otp.php`, requestOptions) .then((response) => response.json()) .then((result) => { if (result.success) { timer = setTimer("login__phone_otp_countdown", "login__resend_phone_otp"); } else { createAlertMessage("login__alert_container", result); } }) .catch((error) => console.error(error)); }; const switchSection = (index) => { sections.forEach((sectionId, idx) => { if (document.getElementById(sectionId)) { if (idx != index) document.getElementById(sectionId).style.display = "none"; else document.getElementById(sectionId).style.display = "block"; } if (index == 0) { document.querySelector(".login__back_icon").style.display = "none"; } else { document.querySelector(".login__back_icon").style.display = "block"; } }); currentSectionIndex = index; }; const onBackClick = () => { document.querySelector("#loginPhoneOtpInput").value = ""; document.querySelector("#loginEmailOtpInput").value = ""; document.querySelectorAll(".login__message").forEach((item) => { item.innerHTML = ""; }); switchSection(currentSectionIndex - 1); }; const onVerifyPhoneLogin = (event) => { event.preventDefault(); const otpElem = document.querySelector("#loginPhoneOtpInput"); const myHeaders = new Headers(); myHeaders.append("Content-Type", "application/x-www-form-urlencoded"); myHeaders.append("Authorization", localStorage.getItem("token")); const urlencoded = new URLSearchParams(); urlencoded.append("phone", enteredPhone); urlencoded.append("otp", otpElem?.value || ""); const requestOptions = { method: "POST", headers: myHeaders, body: urlencoded, redirect: "follow", }; fetch(`${getApiLink()}/api/login/login_validate_phone.php`, requestOptions) .then((response) => response.json()) .then((result) => { if (result.success) { otpElem.value = ""; if (result.status == "token_found") { localStorage.setItem("token", result?.data?.token || ""); redirectToPage(result?.data?.token || ""); } else { localStorage.setItem( "loginMessageRedirect", "Account created successfully, continue to login." ); } } else { createAlertMessage("login__alert_container", result); } }) .catch((error) => console.error(error)); }; const onForgotPassword = () => { const popup = document.createElement("popup-element"); popup.setAttribute("data-header", "Forgot Password"); popup.innerHTML = /*html*/ `