37 lines
1.6 KiB
JavaScript
37 lines
1.6 KiB
JavaScript
// web/view-claim.mjs -- the invite claim: the only public screen.
|
|
// A single-use invite code is exchanged for a persistent token; the
|
|
// display name is a freeform label, collisions allowed.
|
|
|
|
import { $ } from './dom.mjs';
|
|
import { api, setToken } from './client.mjs';
|
|
import { state } from './state.mjs';
|
|
import { banner } from './chrome.mjs';
|
|
import { logEvent } from './log.mjs';
|
|
import { navigate } from './router.mjs';
|
|
|
|
/** @param {?string} arg @param {number} seq @returns {Promise<void>} */
|
|
export async function enter(arg, seq) {
|
|
/** @type {HTMLInputElement} */ ($('claim-code')).value = '';
|
|
/** @type {HTMLInputElement} */ ($('claim-name')).value = '';
|
|
let fresh = false;
|
|
try { fresh = (await api('/session')).fresh; } catch { /* public: works signed out; a dead server shows the plain hint */ }
|
|
if (seq !== state.seq) return;
|
|
$('claim-hint').textContent = fresh
|
|
? 'this deployment has no users yet -- the superuser invite is in state/bootstrap-invite.txt on the server'
|
|
: 'a single-use invite code, from the operator';
|
|
/** @type {HTMLInputElement} */ ($('claim-code')).focus();
|
|
}
|
|
|
|
$('claim-form').addEventListener('submit', async (e) => {
|
|
e.preventDefault();
|
|
try {
|
|
const r = await api('/claim', { method: 'POST', body: { code: /** @type {HTMLInputElement} */ ($('claim-code')).value.trim(), name: /** @type {HTMLInputElement} */ ($('claim-name')).value.trim() } });
|
|
setToken(r.token);
|
|
state.me = r.user;
|
|
logEvent('nav', `claimed: ${r.user.name}${r.user.superuser ? ' (op)' : ''}`);
|
|
navigate('/');
|
|
} catch (err) {
|
|
banner(err.message);
|
|
}
|
|
});
|