let currentEmail = ''; document.addEventListener('DOMContentLoaded', function() { const token = localStorage.getItem('dashx_token'); if (token) { window.location.href = '/dashboard'; } document.getElementById('loginForm').addEventListener('submit', handleLogin); document.getElementById('registerForm').addEventListener('submit', handleRegister); document.getElementById('adminForm').addEventListener('submit', handleAdminLogin); document.getElementById('verifyForm').addEventListener('submit', handleVerify); }); function switchTab(tab) { document.querySelectorAll('.tab-btn').forEach(btn => btn.classList.remove('active')); document.querySelectorAll('.auth-form').forEach(form => form.style.display = 'none'); event.target.classList.add('active'); document.getElementById(tab + 'Form').style.display = 'block'; } async function handleLogin(e) { e.preventDefault(); const formData = new FormData(e.target); const data = Object.fromEntries(formData); try { const response = await fetch('/api/auth/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }); const result = await response.json(); if (result.success) { localStorage.setItem('dashx_token', result.token); localStorage.setItem('dashx_user', JSON.stringify(result.user)); Swal.fire({ title: "Login Successful!", text: "Welcome back to DashX!", icon: "success" }).then(() => { window.location.href = '/dashboard'; }); } else { Swal.fire({ icon: "error", title: "Login Failed", text: result.error }); } } catch (error) { Swal.fire({ icon: "error", title: "Network Error", text: "Please check your connection and try again" }); } } async function handleRegister(e) { e.preventDefault(); const formData = new FormData(e.target); const data = Object.fromEntries(formData); currentEmail = data.email; try { const response = await fetch('/api/auth/register', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }); const result = await response.json(); if (result.success) { Swal.fire({ title: "Registration Successful!", text: "Please check your email for verification code", icon: "success" }); document.getElementById('verifyForm').querySelector('input[name="email"]').value = currentEmail; switchTabDirect('verify'); } else { if (result.error.includes('Multiple accounts')) { Swal.fire({ title: "Account Deleted!", text: "You're account doesn't follow our terms of service, so we deleted this account!", icon: "warning" }); } else { Swal.fire({ icon: "error", title: "Registration Failed", text: result.error, footer: 'Why do I have this issue?' }); } } } catch (error) { Swal.fire({ icon: "error", title: "Network Error", text: "Please check your connection and try again", footer: 'Why do I have this issue?' }); } } async function handleAdminLogin(e) { e.preventDefault(); const formData = new FormData(e.target); const data = Object.fromEntries(formData); try { const response = await fetch('/api/auth/admin-login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }); const result = await response.json(); if (result.success) { localStorage.setItem('dashx_token', result.token); localStorage.setItem('dashx_user', JSON.stringify(result.user)); Swal.fire({ title: "Admin Login Successful!", text: "Welcome to DashX Admin Panel!", icon: "success" }).then(() => { window.location.href = '/dashboard'; }); } else { Swal.fire({ icon: "error", title: "Admin Login Failed", text: result.error }); } } catch (error) { Swal.fire({ icon: "error", title: "Network Error", text: "Please check your connection and try again" }); } } async function handleVerify(e) { e.preventDefault(); const formData = new FormData(e.target); const data = Object.fromEntries(formData); try { const response = await fetch('/api/auth/verify', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }); const result = await response.json(); if (result.success) { localStorage.setItem('dashx_token', result.token); localStorage.setItem('dashx_user', JSON.stringify(result.user)); Swal.fire({ title: "Account Created Successfully!", text: "Welcome to DashX!", icon: "success" }).then(() => { window.location.href = '/dashboard'; }); } else { Swal.fire({ icon: "error", title: "Verification Failed", text: result.error }); } } catch (error) { Swal.fire({ icon: "error", title: "Network Error", text: "Please check your connection and try again" }); } } function switchTabDirect(tab) { document.querySelectorAll('.tab-btn').forEach(btn => btn.classList.remove('active')); document.querySelectorAll('.auth-form').forEach(form => form.style.display = 'none'); document.getElementById(tab + 'Form').style.display = 'block'; } function showErrorDetails(error) { let details = ''; if (error.includes('already exists')) { details = 'An account with this email or username already exists. Please use different credentials or login instead.'; } else if (error.includes('Network')) { details = 'There was a problem connecting to our servers. Please check your internet connection and try again.'; } else if (error.includes('Multiple accounts')) { details = 'Our system detected multiple accounts from the same IP address, which violates our terms of service.'; } else { details = error; } Swal.fire({ title: 'Error Details', text: details, icon: 'info' }); }