Spaces:
Sleeping
Sleeping
File size: 7,253 Bytes
8d22540 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | 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: '<a href="#" onclick="showErrorDetails(\'' + result.error + '\')">Why do I have this issue?</a>'
});
}
}
} catch (error) {
Swal.fire({
icon: "error",
title: "Network Error",
text: "Please check your connection and try again",
footer: '<a href="#" onclick="showErrorDetails(\'Network connection failed\')">Why do I have this issue?</a>'
});
}
}
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'
});
} |