45 lines
1.1 KiB
JavaScript
45 lines
1.1 KiB
JavaScript
import { apiPost, authFetchStatus } from '../api.js';
|
|
|
|
const loadingEl = document.getElementById('page-loading');
|
|
const panel = document.getElementById('login-panel');
|
|
|
|
async function init() {
|
|
let st;
|
|
try {
|
|
st = await authFetchStatus();
|
|
} catch {
|
|
st = { needsBootstrap: false, loggedIn: false };
|
|
}
|
|
if (st.needsBootstrap) {
|
|
location.href = '/bootstrap.html';
|
|
return;
|
|
}
|
|
if (st.loggedIn) {
|
|
location.href = '/start.html';
|
|
return;
|
|
}
|
|
|
|
loadingEl.hidden = true;
|
|
panel.hidden = false;
|
|
|
|
document.getElementById('form-login').onsubmit = async (e) => {
|
|
e.preventDefault();
|
|
const fd = new FormData(e.target);
|
|
try {
|
|
await apiPost('/auth/login', {
|
|
username: fd.get('username'),
|
|
password: fd.get('password'),
|
|
});
|
|
location.href = '/start.html';
|
|
} catch (err) {
|
|
document.querySelector('.auth-err')?.remove();
|
|
const p = document.createElement('p');
|
|
p.className = 'error auth-err';
|
|
p.textContent = err.message || 'Anmeldung fehlgeschlagen';
|
|
document.getElementById('form-login').prepend(p);
|
|
}
|
|
};
|
|
}
|
|
|
|
init();
|