Squash-merges feature from PR #588 by @vcavichini. Dynamic <base href> injection + api() helper slash-stripping enables deploying hermes-webui behind a reverse proxy at any subpath without configuration. Also fixes pre-existing bug: api/upload was using location.origin instead of location.href (closes #596). Co-authored-by: vcavichini <vcavichini@users.noreply.github.com>
56 lines
1.5 KiB
JavaScript
56 lines
1.5 KiB
JavaScript
/* Login page — external script, no inline handlers.
|
|
* Loaded by the /login route. Reads data attributes from the form for
|
|
* i18n strings so the server does not need to inject JS literals.
|
|
*/
|
|
document.addEventListener('DOMContentLoaded', function () {
|
|
var form = document.getElementById('login-form');
|
|
var input = document.getElementById('pw');
|
|
|
|
if (!form || !input) return;
|
|
|
|
var invalidPw = form.getAttribute('data-invalid-pw') || 'Invalid password';
|
|
var connFailed = form.getAttribute('data-conn-failed') || 'Connection failed';
|
|
|
|
function showErr(msg) {
|
|
var err = document.getElementById('err');
|
|
if (err) { err.textContent = msg; err.style.display = 'block'; }
|
|
}
|
|
|
|
function hideErr() {
|
|
var err = document.getElementById('err');
|
|
if (err) { err.style.display = 'none'; }
|
|
}
|
|
|
|
async function doLogin(e) {
|
|
e.preventDefault();
|
|
var pw = input.value;
|
|
hideErr();
|
|
try {
|
|
var res = await fetch('api/auth/login', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ password: pw }),
|
|
credentials: 'include',
|
|
});
|
|
var data = {};
|
|
try { data = await res.json(); } catch (_) {}
|
|
if (res.ok && data.ok) {
|
|
window.location.href = './';
|
|
} else {
|
|
showErr(data.error || invalidPw);
|
|
}
|
|
} catch (ex) {
|
|
showErr(connFailed);
|
|
}
|
|
}
|
|
|
|
form.addEventListener('submit', doLogin);
|
|
|
|
input.addEventListener('keydown', function (e) {
|
|
if (e.key === 'Enter') {
|
|
e.preventDefault();
|
|
doLogin(e);
|
|
}
|
|
});
|
|
});
|