Files
MrTerm/src/web/public/login.js
T

37 lines
1.7 KiB
JavaScript

// Anmeldung bzw. Ersteinrichtung (Admin-Konto) der Web-Version
(async () => {
'use strict';
I18N.setLanguage('auto', navigator.language);
const T = I18N.t;
const $ = (s) => document.querySelector(s);
document.querySelectorAll('[data-t]').forEach((e) => { e.textContent = T(e.dataset.t); });
const st = await fetch('/api/state').then((r) => r.json());
if (st.user) { location.href = '/app/src/renderer/index.html'; return; }
const setup = st.setup;
$('#ver').textContent = `MrTerm Web ${st.version}`;
$('#intro').textContent = setup ? T('Welcome! Create the administrator account for this MrTerm server.') : T('Sign in to your MrTerm server.');
$('#submit').textContent = setup ? T('Create account') : T('Sign in');
$('#repeatRow').hidden = !setup;
if (setup) $('[name=password]').autocomplete = 'new-password';
$('#form').onsubmit = async (e) => {
e.preventDefault();
const f = Object.fromEntries(new FormData(e.target));
const err = $('#err');
err.hidden = true;
if (setup && f.password !== f.repeat) { err.textContent = T('The passwords do not match.'); err.hidden = false; return; }
$('#submit').disabled = true;
try {
const r = await fetch(setup ? '/api/setup' : '/api/login', { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-MrTerm': '1' }, body: JSON.stringify({ name: f.name, password: f.password }) });
const j = await r.json().catch(() => ({}));
if (!r.ok) throw new Error(j.error || r.statusText);
location.href = '/app/src/renderer/index.html';
} catch (ex) {
err.textContent = T(ex.message);
err.hidden = false;
$('#submit').disabled = false;
}
};
})();