Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- static/script.js +40 -0
static/script.js
CHANGED
|
@@ -44,6 +44,46 @@ document.getElementById('create-form').addEventListener('submit', async (e) => {
|
|
| 44 |
}
|
| 45 |
});
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
async function loadLogs() {
|
| 48 |
const res = await fetch('/api/cms/logs');
|
| 49 |
const logs = await res.json();
|
|
|
|
| 44 |
}
|
| 45 |
});
|
| 46 |
|
| 47 |
+
document.getElementById('login-form').addEventListener('submit', async (e) => {
|
| 48 |
+
e.preventDefault();
|
| 49 |
+
|
| 50 |
+
const messageContainer = document.getElementById('message-container');
|
| 51 |
+
messageContainer.innerHTML = '<div style="background:#fff3cd;border:2px solid #ffc107;padding:1rem;margin-bottom:1rem;border-radius:4px;text-align:center;">Logging in...</div>';
|
| 52 |
+
|
| 53 |
+
try {
|
| 54 |
+
const res = await fetch('/login', {
|
| 55 |
+
method: 'POST',
|
| 56 |
+
headers: {'Content-Type': 'application/json'},
|
| 57 |
+
body: JSON.stringify({
|
| 58 |
+
username: document.getElementById('username').value,
|
| 59 |
+
password: document.getElementById('password').value
|
| 60 |
+
}),
|
| 61 |
+
credentials: 'include' // Important! Include cookies
|
| 62 |
+
});
|
| 63 |
+
|
| 64 |
+
const contentType = res.headers.get('content-type');
|
| 65 |
+
if (!contentType || !contentType.includes('application/json')) {
|
| 66 |
+
throw new Error('Server returned HTML instead of JSON. Check backend logs.');
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
const data = await res.json();
|
| 70 |
+
|
| 71 |
+
if (res.ok) {
|
| 72 |
+
messageContainer.innerHTML = '<div class="success-message">✅ Login successful! Redirecting...</div>';
|
| 73 |
+
|
| 74 |
+
// Force redirect to dashboard
|
| 75 |
+
setTimeout(() => {
|
| 76 |
+
window.location.href = '/';
|
| 77 |
+
}, 1000);
|
| 78 |
+
} else {
|
| 79 |
+
messageContainer.innerHTML = `<div class="error-message">❌ ${data.message}</div>`;
|
| 80 |
+
}
|
| 81 |
+
} catch (error) {
|
| 82 |
+
console.error('Login error:', error);
|
| 83 |
+
messageContainer.innerHTML = `<div class="error-message">❌ ${error.message}</div>`;
|
| 84 |
+
}
|
| 85 |
+
});
|
| 86 |
+
|
| 87 |
async function loadLogs() {
|
| 88 |
const res = await fetch('/api/cms/logs');
|
| 89 |
const logs = await res.json();
|