From ceb4f6fa2ebde596b47648ef464c14d0a7a8c44d Mon Sep 17 00:00:00 2001 From: Atlaskor Date: Thu, 13 Nov 2025 15:33:44 +0000 Subject: [PATCH] Add server/static/ui_auth.js --- server/static/ui_auth.js | 47 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 server/static/ui_auth.js diff --git a/server/static/ui_auth.js b/server/static/ui_auth.js new file mode 100644 index 0000000..2b3162e --- /dev/null +++ b/server/static/ui_auth.js @@ -0,0 +1,47 @@ +export function setupAuthPanel(root, socket, onLogin) { + root.innerHTML = ` + + `; + const panel = root.firstElementChild; + const user = panel.querySelector('#auth-user'); + const pass = panel.querySelector('#auth-pass'); + const msg = panel.querySelector('#auth-msg'); + + async function call(path) { + const res = await fetch(path, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ username: user.value, password: pass.value }) + }); + return res; + } + + panel.querySelector('#auth-register').onclick = async () => { + const r = await call('/api/register'); + if (r.ok) { + panel.classList.add('hidden'); + onLogin(); + } else { + msg.textContent = 'Username taken.'; + } + }; + + panel.querySelector('#auth-login').onclick = async () => { + const r = await call('/api/login'); + if (r.ok) { + panel.classList.add('hidden'); + onLogin(); + } else { + msg.textContent = 'Invalid login.'; + } + }; +}