calorie-ai-android/web/public/login.js
Daniel f57c04c989
All checks were successful
Android CI / build (push) Successful in 31s
Web CI / test (push) Successful in 46s
Add web auth and app navigation
2026-05-19 17:59:02 +02:00

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;
}
});