Repository navigation
Expand file tree
/
Copy pathapi.js
More file actions
618 lines (539 loc) · 24.1 KB
/
Copy pathapi.js
File metadata and controls
618 lines (539 loc) · 24.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
// api.js — the "Willow" bridge between vwag-table and the VWAG FastAPI on fallon.
//
// Self-contained module (Path A): it owns the whole login/register feature —
// transport, the toolbar button, the modal, its styles, and all wiring. To
// remove the feature, delete this file and its <script> tag in index.html.
// Nothing in state.js / main.js / styles.css depends on it.
//
// Transport surface (consumed here today, and by the NEXT Willow chunk that
// fetches settlement NPCs):
// login(username, password) -> { ok, username?, error? }
// register(username, email, pass) -> { ok, username?, error? }
// logout() -> clears the stored token
// isLoggedIn() getUsername() getToken() authHeader()
// apiFetch(path, opts) -> fetch wrapper: base URL + Bearer + JSON
//
// Auth model (server side, shipped): the token IS the player's unique_key, a
// plaintext UUID. POST /api/auth/login returns it; we store it and send it as
// `Authorization: Bearer <unique_key>` on every guarded call. See VWAG's
// docs/source/security.md.
//
// Base URL for the FastAPI on fallon — context-aware, ONE client, two serving
// contexts:
// - served FROM game.worhl.net (the online tier / player view, the remote
// guest, or a GM using the hosted client): same-origin, so the base is ""
// and every call is a relative /api/... that Apache proxies to uvicorn — no
// host baked in, identical on the LAN and over the public TLS vhost. A public
// guest MUST have this; the LAN IP is unreachable from off-site.
// - served locally for GM dev (python3 -m http.server on jedas): a DIFFERENT
// origin from the API, so it needs the absolute LAN address. This preserves
// the GM's direct, off-grid LAN path instead of routing it through Apache.
//
// Chunk 4 (online tier): this module also registers a remote content source into
// content.js's resolver (fetchModule / fetchImage). Any map or image not found
// locally is then pulled from fallon and cached down into IndexedDB — fallon is
// the source of truth, but once a map has been opened it plays fully off-grid.
import { registerRemoteSource } from "./content.js";
const FALLON_BASE =
window.location.hostname === "game.worhl.net" ? "" : "http://10.10.0.10:8002";
const STORE_KEY = "vwag.unique_key";
const STORE_USER = "vwag.username";
const STORE_ADMIN = "vwag.is_admin";
// GM view only — the player display (?view=player) never shows login.
const IS_PLAYER =
new URLSearchParams(window.location.search).get("view") === "player";
// ── token state ──────────────────────────────────────────────────────────────
let _token = null;
let _username = null;
let _isAdmin = false; // whether the logged-in player may delete any map (whoami)
function _loadStored() {
try {
_token = localStorage.getItem(STORE_KEY) || null;
_username = localStorage.getItem(STORE_USER) || null;
_isAdmin = localStorage.getItem(STORE_ADMIN) === "1";
} catch {
_token = _username = null; // private mode / storage disabled
_isAdmin = false;
}
}
function _store(token, username) {
_token = token;
_username = username;
try {
if (token) {
localStorage.setItem(STORE_KEY, token);
localStorage.setItem(STORE_USER, username || "");
} else {
localStorage.removeItem(STORE_KEY);
localStorage.removeItem(STORE_USER);
localStorage.removeItem(STORE_ADMIN);
}
} catch {
/* non-persistent session is acceptable; in-memory token still works */
}
if (!token) _isAdmin = false; // logging out drops admin rights
}
export function getToken() { return _token; }
export function getUsername() { return _username; }
export function isLoggedIn() { return !!_token; }
export function isAdmin() { return _isAdmin; }
export function logout() {
_store(null, null);
_refreshButton();
}
export function authHeader() {
return _token ? { Authorization: `Bearer ${_token}` } : {};
}
// ── ephemeral guest identity (online tier, Chunk 1) ──────────────────────────
// The player view has no login of its own. A remote guest is handed in by the
// join gate (landing.html), which redeems a code and stashes the contract in
// sessionStorage before redirecting to ?view=player. Seat that token in-memory
// so authHeader() carries it — the Chunk 2 relay authenticates the socket with
// it. Returns the seated identity { token, name, gameId } or null if there was
// no stash (a normal local player view, or storage disabled).
//
// Deliberately NOT _store(): the guest identity is ephemeral (it dies with the
// tab) and must never touch the persistent localStorage the GM login uses.
// Guests are never admins.
export function adoptStoredGuest() {
let raw;
try {
raw = sessionStorage.getItem("vwag.guest");
} catch {
return null; // storage disabled / private mode
}
if (!raw) return null;
let guest;
try {
guest = JSON.parse(raw);
} catch {
return null; // corrupt stash — ignore rather than throw into boot
}
if (!guest || !guest.token) return null;
_token = guest.token;
_username = guest.name || null;
_isAdmin = false;
return { token: _token, name: _username, gameId: guest.gameId ?? null };
}
// ── transport ────────────────────────────────────────────────────────────────
// Pull a human-readable message out of a FastAPI error body. `detail` is a
// string for our HTTPExceptions (401/409/400) and a list for 422 validation.
function _errorMessage(body, status) {
const d = body && body.detail;
if (typeof d === "string") return d;
if (Array.isArray(d) && d.length) {
const first = d[0];
if (first && typeof first.msg === "string") {
// e.g. "String should have at least 6 characters" on the password field.
const field = Array.isArray(first.loc) ? first.loc[first.loc.length - 1] : "";
return field ? `${field}: ${first.msg}` : first.msg;
}
}
return `Request failed (${status}).`;
}
async function _postJson(path, payload) {
let resp;
try {
resp = await fetch(`${FALLON_BASE}${path}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload),
});
} catch {
// Network error — fallon down / off-grid / wrong host. Distinct from auth.
return { ok: false, error: "Can't reach Victen Worhl. Is Fallon online?" };
}
let body = null;
try { body = await resp.json(); } catch { /* empty or non-JSON body */ }
if (!resp.ok) return { ok: false, error: _errorMessage(body, resp.status) };
return { ok: true, body };
}
export async function login(username, password) {
const r = await _postJson("/api/auth/login", { username, password });
if (!r.ok) return r;
_store(r.body.unique_key, r.body.username);
await refreshWhoami();
return { ok: true, username: r.body.username };
}
export async function register(username, email, password) {
const r = await _postJson("/api/auth/register", { username, email, password });
if (!r.ok) return r;
_store(r.body.unique_key, r.body.username);
await refreshWhoami();
return { ok: true, username: r.body.username };
}
// Confirm identity + admin flag against fallon. Best-effort: a failure (offline,
// stale token) just leaves is_admin false rather than throwing into the caller.
// Persisted so a returning admin's rights are known before the call resolves.
export async function refreshWhoami() {
if (!isLoggedIn()) { _isAdmin = false; return; }
try {
const me = await apiFetch("/api/vtt/whoami");
_isAdmin = !!(me && me.is_admin);
try { localStorage.setItem(STORE_ADMIN, _isAdmin ? "1" : "0"); } catch {}
} catch {
/* leave the persisted/in-memory value as-is on a transient failure */
}
}
// DELETE /api/vtt/modules/{id}. Guarded server-side (owner or admin); apiFetch
// carries the Bearer and throws with .status set (403 not yours / 404 gone / 401).
export async function deleteRemoteModule(id) {
return apiFetch(`/api/vtt/modules/${encodeURIComponent(id)}`, { method: "DELETE" });
}
// General authed fetch for the NEXT chunk (settlement NPC pulls). Returns
// parsed JSON, or throws an Error tagged with .status on a non-2xx so callers
// can branch on 401 (token went stale) vs other failures.
export async function apiFetch(path, opts = {}) {
const resp = await fetch(`${FALLON_BASE}${path}`, {
...opts,
headers: { ...authHeader(), ...(opts.headers || {}) },
});
if (!resp.ok) {
const err = new Error(`apiFetch ${path} -> ${resp.status}`);
err.status = resp.status;
throw err;
}
return resp.status === 204 ? null : resp.json();
}
// ── remote content source (the resolver's fallon tier) ───────────────────────
// content.js's resolver calls these when a module/image isn't found locally and
// caches any hit down into IndexedDB, so the next resolve — and all off-grid
// play — is local. A thrown error (404 / network) is the resolver's signal to
// fall through to local-only, so these don't swallow failures themselves.
function _blobToDataURL(blob) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result);
reader.onerror = () => reject(reader.error);
reader.readAsDataURL(blob);
});
}
const remoteSource = {
// GET /api/vtt/modules/{id} -> { ..., data: <browser module record> }. The
// resolver wants the record itself, which fallon stores in the `data` column.
async fetchModule(id) {
const row = await apiFetch(`/api/vtt/modules/${encodeURIComponent(id)}`);
return row && row.data ? row.data : null;
},
// GET /api/vtt/images/{id} -> { ..., url }. Bytes live at FALLON_BASE+url
// (/static/vtt/...). Pull them and hand back a dataURL — the form the image
// store and renderer use.
async fetchImage(imageId) {
const meta = await apiFetch(`/api/vtt/images/${encodeURIComponent(imageId)}`);
if (!meta || !meta.url) return "";
const resp = await fetch(`${FALLON_BASE}${meta.url}`);
if (!resp.ok) return "";
return _blobToDataURL(await resp.blob());
},
};
// The map catalog for the "From fallon" picker — lightweight rows (no data blob):
// [{ id, name, map_kind, parent_id, updated_at }]. Returns [] on any failure
// (off-grid / fallon down) so the picker shows empty instead of throwing.
export async function listRemoteModules() {
try {
const rows = await apiFetch("/api/vtt/modules");
if (!Array.isArray(rows)) return [];
// Absolutize the cover thumb URL so the picker can use it directly as an <img>
// src (keeps FALLON_BASE here, where the host concern already lives).
return rows.map((r) => ({
...r,
thumbnail_url: r.thumbnail_url ? `${FALLON_BASE}${r.thumbnail_url}` : null,
}));
} catch {
return [];
}
}
// ── publish (write path: browser → fallon) ───────────────────────────────────
// The mirror of the remote source above: these push a locally authored map UP to
// fallon. Guarded server-side (require_player), so apiFetch must carry a Bearer —
// callers gate on isLoggedIn() first. apiFetch throws on non-2xx with .status set,
// so a 401 (token went stale) is distinguishable from fallon being unreachable.
// PUT /api/vtt/modules/{id}. Body mirrors the columns fallon extracts; `data` is
// the whole browser module record, stored verbatim so a later pull round-trips.
// `thumbnail` is the served path of the cover thumb (already pushed via
// putRemoteImage), so the From-fallon picker can show it without pulling the map.
export async function publishModule(record, thumbnail = null) {
return apiFetch(`/api/vtt/modules/${encodeURIComponent(record.id)}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
name: record.name ?? null,
map_kind: record.mapKind || "battle",
parent_id: record.parentId ?? null,
data: record,
thumbnail: thumbnail ?? null,
}),
});
}
// PUT /api/vtt/images/{id}. The browser holds image bytes as dataURLs already, so
// we ship the dataURL as-is; fallon decodes it to disk. Named putRemoteImage to
// avoid colliding with db.js's local putImage (both are imported into main.js).
export async function putRemoteImage(imageId, dataURL, width, height) {
return apiFetch(`/api/vtt/images/${encodeURIComponent(imageId)}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
data_url: dataURL,
width: width ?? null,
height: height ?? null,
}),
});
}
// PUT /api/vtt/sessions/{id}. The play-state checkpoint — the session record's
// full JSON in `data`, with module_id (the FK) + name as columns. fallon 400s if
// the module isn't published yet; the checkpoint flow publishes it first.
export async function publishSession(record) {
return apiFetch(`/api/vtt/sessions/${encodeURIComponent(record.id)}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
module_id: record.moduleId || record.id,
name: record.name ?? null,
data: record,
}),
});
}
// True if a module already lives on fallon — drives the overwrite confirm. A 404
// means "new, no confirm needed"; any other error propagates so the caller can
// treat it as fallon-unreachable.
export async function remoteModuleExists(id) {
try {
await apiFetch(`/api/vtt/modules/${encodeURIComponent(id)}`);
return true;
} catch (err) {
if (err.status === 404) return false;
throw err;
}
}
// GET /api/vtt/sessions/{id} → the checkpointed session record (the browser's
// session JSON in `data`), or throws .status 404 if fallon has no saved session
// for this map. Open endpoint (read), so it works without a Bearer.
export async function fetchRemoteSession(id) {
const meta = await apiFetch(`/api/vtt/sessions/${encodeURIComponent(id)}`);
return meta ? meta.data : null;
}
// Light up the resolver's fallon tier. The player view (?view=player) receives
// its state over BroadcastChannel and never resolves content itself, so it stays
// network-silent — only the GM side registers.
if (!IS_PLAYER) registerRemoteSource(remoteSource);
// ── injected styles ──────────────────────────────────────────────────────────
const STYLES = `
.vwag-login-dialog {
background: var(--panel);
border: 1px solid var(--line);
border-radius: 12px;
color: var(--text);
padding: 0;
width: min(92vw, 360px);
}
.vwag-login-dialog::backdrop { background: rgba(0, 0, 0, 0.55); }
.vwag-login-dialog form { display: grid; gap: 14px; margin: 0; padding: 22px; }
.vwag-login-brand { text-align: center; display: grid; gap: 2px; margin-bottom: 4px; }
.vwag-login-brand .wordmark {
font-family: Georgia, "Times New Roman", serif;
font-size: 1.6rem; font-weight: 600; letter-spacing: 1px;
color: var(--brand-bright); margin: 0;
}
.vwag-login-brand .subtitle {
font-style: italic; font-size: 0.85rem; color: var(--muted); margin: 0;
}
.vwag-login-field { display: grid; gap: 5px; }
.vwag-login-field label { font-size: 0.8rem; color: var(--muted); }
.vwag-login-dialog input {
background: var(--bg);
border: 1px solid var(--line);
border-radius: 6px; color: var(--text);
min-height: 40px; padding: 0 12px; width: 100%;
font-size: 0.95rem;
}
.vwag-login-dialog input:focus { outline: none; border-color: var(--brand-bright); }
.vwag-login-field.hidden { display: none; }
.vwag-login-pw { display: grid; grid-template-columns: 1fr auto; gap: 8px; }
.vwag-login-eye {
background: var(--brand); border: none; border-radius: 6px;
color: var(--on-brand); cursor: pointer; width: 44px;
display: grid; place-items: center;
}
.vwag-login-eye svg { width: 18px; height: 18px; stroke: currentColor; fill: none; stroke-width: 2; }
.vwag-login-status { min-height: 1.1em; font-size: 0.82rem; color: var(--muted); text-align: center; }
.vwag-login-status.error { color: var(--danger); }
.vwag-login-status.success { color: var(--brand-bright); }
.vwag-login-submit {
background: var(--brand); border: none; border-radius: 8px;
color: var(--on-brand); cursor: pointer; font-weight: 750;
min-height: 44px; font-size: 0.95rem;
}
.vwag-login-submit:disabled { opacity: 0.6; cursor: default; }
.vwag-login-toggle { text-align: center; font-size: 0.8rem; color: var(--muted); }
.vwag-login-toggle button {
background: none; border: none; color: var(--brand-bright);
cursor: pointer; font-size: 0.8rem; padding: 0; text-decoration: underline;
}
.top-action.vwag-logged-in svg { stroke: var(--brand-bright); }
`;
const EYE_ICON =
`<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M2 12s3-7 10-7 10 7 10 7-3 7-10 7-10-7-10-7z"/><circle cx="12" cy="12" r="3"/></svg>`;
const LOGIN_ICON =
`<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M15 3h4a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2h-4"/><polyline points="10 17 15 12 10 7"/><line x1="15" x2="3" y1="12" y2="12"/></svg>`;
const USER_ICON =
`<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><polyline points="16 11 18 13 22 9"/></svg>`;
// ── injected DOM ─────────────────────────────────────────────────────────────
let btn, dialog, statusEl, submitBtn, toggleBtn, toggleText,
userInput, emailInput, pwInput, emailField, headingSubtitle;
let mode = "login"; // "login" | "register"
function _injectStyles() {
const el = document.createElement("style");
el.id = "vwag-login-styles";
el.textContent = STYLES;
document.head.appendChild(el);
}
function _injectButton() {
const bar = document.querySelector(".top-actions");
if (!bar) return false;
btn = document.createElement("button");
btn.id = "vwagLoginBtn";
btn.type = "button";
btn.className = "top-action";
bar.appendChild(btn);
btn.addEventListener("click", _onButtonClick);
return true;
}
function _injectDialog() {
dialog = document.createElement("dialog");
dialog.id = "vwagLoginDialog";
dialog.className = "vwag-login-dialog";
dialog.innerHTML = `
<form id="vwagLoginForm">
<div class="vwag-login-brand">
<p class="wordmark">Victen Worhl</p>
<p class="subtitle" id="vwagLoginSubtitle">Adventure Game</p>
</div>
<div class="vwag-login-field">
<label for="vwagLoginUser">Username</label>
<input id="vwagLoginUser" type="text" autocomplete="username" autocapitalize="off" spellcheck="false">
</div>
<div class="vwag-login-field hidden" id="vwagLoginEmailField">
<label for="vwagLoginEmail">Email</label>
<input id="vwagLoginEmail" type="email" autocomplete="email" autocapitalize="off" spellcheck="false">
</div>
<div class="vwag-login-field">
<label for="vwagLoginPw">Password</label>
<div class="vwag-login-pw">
<input id="vwagLoginPw" type="password" autocomplete="current-password">
<button type="button" class="vwag-login-eye" id="vwagLoginEye" title="Show password" aria-label="Show password">${EYE_ICON}</button>
</div>
</div>
<p class="vwag-login-status" id="vwagLoginStatus"></p>
<button type="submit" class="vwag-login-submit" id="vwagLoginSubmit">Sign In</button>
<p class="vwag-login-toggle" id="vwagLoginToggleWrap">
<span id="vwagLoginToggleText">Don't have an account?</span>
<button type="button" id="vwagLoginToggle">Sign Up</button>
</p>
</form>
`;
document.body.appendChild(dialog);
statusEl = dialog.querySelector("#vwagLoginStatus");
submitBtn = dialog.querySelector("#vwagLoginSubmit");
toggleBtn = dialog.querySelector("#vwagLoginToggle");
toggleText = dialog.querySelector("#vwagLoginToggleText");
userInput = dialog.querySelector("#vwagLoginUser");
emailInput = dialog.querySelector("#vwagLoginEmail");
pwInput = dialog.querySelector("#vwagLoginPw");
emailField = dialog.querySelector("#vwagLoginEmailField");
headingSubtitle = dialog.querySelector("#vwagLoginSubtitle");
dialog.querySelector("#vwagLoginForm").addEventListener("submit", _onSubmit);
toggleBtn.addEventListener("click", _toggleMode);
dialog.querySelector("#vwagLoginEye").addEventListener("click", _toggleEye);
}
// ── UI behavior ──────────────────────────────────────────────────────────────
function _setStatus(msg, kind) {
statusEl.textContent = msg || "";
statusEl.className = "vwag-login-status" + (kind ? ` ${kind}` : "");
}
function _setMode(next) {
mode = next;
const reg = mode === "register";
emailField.classList.toggle("hidden", !reg);
submitBtn.textContent = reg ? "Create Account" : "Sign In";
toggleText.textContent = reg ? "Have an account?" : "Don't have an account?";
toggleBtn.textContent = reg ? "Sign In" : "Sign Up";
headingSubtitle.textContent = reg ? "Create your account" : "Adventure Game";
pwInput.autocomplete = reg ? "new-password" : "current-password";
_setStatus("");
}
function _toggleMode() { _setMode(mode === "login" ? "register" : "login"); }
function _toggleEye() {
const showing = pwInput.type === "text";
pwInput.type = showing ? "password" : "text";
const eye = dialog.querySelector("#vwagLoginEye");
eye.title = showing ? "Show password" : "Hide password";
eye.setAttribute("aria-label", eye.title);
}
function _refreshButton() {
// Single auth chokepoint: called on login success, logout, and init. Broadcast
// the current auth state so the rest of the app (e.g. the Online session sidebar
// section) can show/hide itself. Fired before the no-button bail so listeners run
// regardless of whether the toolbar button exists.
document.dispatchEvent(new CustomEvent("vwag:auth", { detail: { loggedIn: isLoggedIn() } }));
if (!btn) return;
if (isLoggedIn()) {
btn.innerHTML = USER_ICON;
btn.classList.add("vwag-logged-in");
btn.title = `Logged in as ${getUsername()} — click to log out`;
} else {
btn.innerHTML = LOGIN_ICON;
btn.classList.remove("vwag-logged-in");
btn.title = "Log in to Victen Worhl";
}
btn.setAttribute("aria-label", btn.title);
}
function _onButtonClick() {
if (isLoggedIn()) {
if (confirm(`Log out ${getUsername()}?`)) logout();
return;
}
_setMode("login");
userInput.value = "";
emailInput.value = "";
pwInput.value = "";
pwInput.type = "password";
dialog.showModal();
userInput.focus();
}
async function _onSubmit(e) {
e.preventDefault();
const username = userInput.value.trim();
const password = pwInput.value;
const email = emailInput.value.trim();
if (!username || !password || (mode === "register" && !email)) {
_setStatus("Please fill in every field.", "error");
return;
}
submitBtn.disabled = true;
_setStatus(mode === "register" ? "Creating account…" : "Signing in…");
const result = mode === "register"
? await register(username, email, password)
: await login(username, password);
submitBtn.disabled = false;
if (!result.ok) {
_setStatus(result.error, "error");
return;
}
_setStatus(`Welcome, ${result.username}!`, "success");
_refreshButton();
setTimeout(() => dialog.close(), 500);
}
// ── init ─────────────────────────────────────────────────────────────────────
function init() {
if (IS_PLAYER) return; // GM view only
_loadStored();
_injectStyles();
if (!_injectButton()) return; // no toolbar (unexpected) → bail quietly
_injectDialog();
_refreshButton();
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", init, { once: true });
} else {
init();
}