31 lines
1 KiB
JavaScript
31 lines
1 KiB
JavaScript
const loginForm = document.getElementById('loginForm');
|
|
const loginStatus = document.getElementById('loginStatus');
|
|
|
|
loginForm.addEventListener('submit', async (event) => {
|
|
event.preventDefault();
|
|
const button = loginForm.querySelector('button');
|
|
button.disabled = true;
|
|
loginStatus.textContent = 'Checking credentials...';
|
|
loginStatus.className = 'status ok';
|
|
|
|
try {
|
|
const response = await fetch('/api/login', {
|
|
method: 'POST',
|
|
headers: { 'content-type': 'application/json' },
|
|
body: JSON.stringify({
|
|
username: document.getElementById('username').value.trim(),
|
|
password: document.getElementById('password').value,
|
|
}),
|
|
});
|
|
if (!response.ok) {
|
|
const error = await response.json().catch(() => ({ error: 'Sign in failed' }));
|
|
throw new Error(error.error || 'Sign in failed');
|
|
}
|
|
window.location.href = '/';
|
|
} catch (error) {
|
|
loginStatus.textContent = error.message;
|
|
loginStatus.className = 'status error';
|
|
} finally {
|
|
button.disabled = false;
|
|
}
|
|
});
|