document.addEventListener("DOMContentLoaded",function(){$('[data-init="toast"]').toast("show");var e=Cookies.get("user_id"),o=Cookies.get("domain"),t=Cookies.get("path");const s=window.location.hostname;var n=window.location.pathname.split("/").slice(0,-1).join("/"),a=localStorage.getItem("twofa_token");if(a&&window.location.pathname.includes("twofa_verify.php")){var r=a;const d=document.getElementById("twofa-form");if(d){const p=document.getElementById("error-message");d.addEventListener("submit",function(e){e.preventDefault();e=document.getElementById("twofa_code").value.trim();if(/^\d{6}$/.test(e)){var o=new FormData;o.append("twofa_code",e),o.append("twofa_token",r);const n=d.querySelector('button[type="submit"]'),a=n.innerHTML;n.disabled=!0,n.innerHTML='<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> Verifying...',fetch("./includes/login.php",{method:"POST",body:o}).then(e=>{if(e.ok)return e.json();throw new Error("Network response was not ok")}).then(e=>{var o,t;n.disabled=!1,n.innerHTML=a,e.success?((o=new Date).setTime(o.getTime()+108e5),window.location.hostname,t=window.location.pathname.split("/").slice(0,-1).join("/"),Cookies.set("user_id",e.user_id,{expires:o,path:"/"}),Cookies.set("username",e.username,{expires:o,path:"/"}),Cookies.set("name",e.name,{expires:o,path:"/"}),Cookies.set("avatar",e.avatar,{expires:o,path:"/"}),Cookies.set("domain",e.domain,{expires:o,path:"/"}),Cookies.set("path",t,{expires:o,path:"/"}),Cookies.set("user_type",e.user_type,{expires:o,path:"/"}),localStorage.removeItem("twofa_token"),"owner"===e.user_type?window.location.href="admin.php":window.location.href="dashboard.php"):(p&&(p.textContent=e.message||"Invalid verification code",p.style.display="block"),document.getElementById("twofa_code").value="",document.getElementById("twofa_code").focus(),e.requires_2fa||(localStorage.removeItem("twofa_token"),setTimeout(()=>{window.location.href="index.php?error="+encodeURIComponent(e.message||"Authentication failed")},2e3)))}).catch(e=>{console.error("Error:",e),n.disabled=!1,n.innerHTML=a,p&&(p.textContent="An error occurred. Please try again.",p.style.display="block")})}else p&&(p.textContent="Please enter a valid 6-digit code",p.style.display="block")})}}else{e&&o===s&&t===n?window.location.href="dashboard.php":(Cookies.remove("user_id"),Cookies.remove("username"),Cookies.remove("name"),Cookies.remove("avatar"),Cookies.remove("domain"),Cookies.remove("path"),Cookies.remove("user_type"),localStorage.removeItem("twofa_token"));const c=document.forms.login_form;if(c){const l=new URLSearchParams(window.location.search).get("redirect")||"dashboard.php";c.addEventListener("submit",function(e){e.preventDefault();var e=c.querySelector('input[name="csrf_token"]')?.value,o=c.username.value,t=c.password.value,n=new FormData;if(e){if(n.append("csrf_token",e),n.append("username",o),n.append("password",t),n.append("redirect",l),document.querySelector(".g-recaptcha")){e=grecaptcha.getResponse();if(!e)return void i("Please complete the reCAPTCHA.");n.append("g-recaptcha-response",e)}const a=c.querySelector('button[type="submit"]'),r=a.innerHTML;a.disabled=!0,a.innerHTML='<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> Signing in...',fetch("./includes/login.php",{method:"POST",body:n}).then(e=>{if(e.ok)return e.json();throw new Error("Network response was not ok")}).then(e=>{var o,t;a.disabled=!1,a.innerHTML=r,e.success?((o=new Date).setTime(o.getTime()+108e5),t=window.location.pathname.split("/").slice(0,-1).join("/"),Cookies.set("user_id",e.user_id,{expires:o,path:"/"}),Cookies.set("username",e.username,{expires:o,path:"/"}),Cookies.set("name",e.name,{expires:o,path:"/"}),Cookies.set("avatar",e.avatar,{expires:o,path:"/"}),Cookies.set("domain",s,{expires:o,path:"/"}),Cookies.set("path",t,{expires:o,path:"/"}),Cookies.set("user_type",e.user_type,{expires:o,path:"/"}),window.location.href=l):e.requires_2fa?e.twofa_token?(localStorage.setItem("twofa_token",e.twofa_token),window.location.href="twofa_verify.php"):i("Missing 2FA verification token"):i(e.message||"An error occurred during login")}).catch(e=>{console.error("Error:",e),a.disabled=!1,a.innerHTML=r,i("An error occurred during login. Please try again.")})}else i("CSRF token missing. Please refresh the page.")});a=document.querySelector(".g-recaptcha");function i(e){var o=document.getElementById("error-message");o&&(o.textContent=e,o.style.display="block")}a&&a.setAttribute("data-theme","dark")}}});
//# sourceMappingURL=login.js.map
