Login button with show-password, skip identical photos on upload
A Přihlásit/Odhlásit button in the top bar replaces logging in via Přidat hru. The dialog has an eye toggle, says when it is checking, and keeps the remember box as it was after a wrong password; nothing is stored until the server accepts it. A remembered password that no longer works is dropped on load. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HA82gVRDXpoukzgMFumoJt
This commit is contained in:
parent
ab842a1d73
commit
6240907809
6 changed files with 138 additions and 44 deletions
|
|
@ -10,6 +10,7 @@ export function setPassword(pw, remember) {
|
|||
if (remember) localStorage.setItem(PASS_KEY, pw); else localStorage.removeItem(PASS_KEY);
|
||||
} catch { /* storage blocked: the password just won't be remembered */ }
|
||||
}
|
||||
const isRemembered = () => { try { return Boolean(localStorage.getItem(PASS_KEY)); } catch { return false; } };
|
||||
export function clearPassword() {
|
||||
try { sessionStorage.removeItem(PASS_KEY); localStorage.removeItem(PASS_KEY); } catch { /* ignore */ }
|
||||
}
|
||||
|
|
@ -52,37 +53,91 @@ export const esc = (s) => String(s).replace(/[&<>"']/g, (c) => ({ "&": "&",
|
|||
export const $ = (id) => document.getElementById(id);
|
||||
|
||||
// Ask for the upload password in a modal dialog; resolves true once logged in, false if cancelled.
|
||||
// The password is stored only after the server accepted it, and only where the
|
||||
// "stay logged in" box says (this tab, or this browser until "Odhlásit").
|
||||
export function askPassword(reason = "") {
|
||||
return new Promise((resolve) => {
|
||||
const dlg = document.createElement("dialog");
|
||||
dlg.className = "login";
|
||||
dlg.innerHTML = `<form method="dialog">
|
||||
<h2>Heslo pro úpravy</h2>
|
||||
dlg.innerHTML = `<form method="dialog" novalidate>
|
||||
<h2>Přihlášení</h2>
|
||||
${reason ? `<p class="muted">${esc(reason)}</p>` : ""}
|
||||
<label class="field"><span>Heslo</span><input type="password" name="pw" autocomplete="current-password" required></label>
|
||||
<label class="radio"><input type="checkbox" name="remember"> Zapamatovat na tomto zařízení</label>
|
||||
<p class="error" hidden></p>
|
||||
<div class="actions"><button value="cancel" formnovalidate>Zrušit</button><button class="primary" value="ok">Přihlásit</button></div>
|
||||
<label class="field"><span>Heslo</span>
|
||||
<span class="pw-field">
|
||||
<input type="password" name="pw" autocomplete="current-password" autocapitalize="off" spellcheck="false" aria-describedby="login-error">
|
||||
<button type="button" class="eye" aria-label="Zobrazit heslo" aria-pressed="false" title="Zobrazit heslo">${EYE}</button>
|
||||
</span>
|
||||
</label>
|
||||
<label class="check"><input type="checkbox" name="remember"> Zůstat přihlášen v tomto prohlížeči</label>
|
||||
<p class="error" id="login-error" role="alert" hidden></p>
|
||||
<div class="actions"><button value="cancel" type="button" data-cancel>Zrušit</button><button class="primary" value="ok" type="submit">Přihlásit</button></div>
|
||||
</form>`;
|
||||
document.body.append(dlg);
|
||||
const form = dlg.querySelector("form");
|
||||
const pw = form.pw;
|
||||
const err = dlg.querySelector(".error");
|
||||
const ok = form.querySelector("button[type=submit]");
|
||||
const eye = form.querySelector(".eye");
|
||||
const showError = (msg) => {
|
||||
err.textContent = msg;
|
||||
err.hidden = !msg;
|
||||
pw.toggleAttribute("aria-invalid", Boolean(msg));
|
||||
};
|
||||
eye.addEventListener("click", () => {
|
||||
const show = pw.type === "password";
|
||||
pw.type = show ? "text" : "password";
|
||||
eye.innerHTML = show ? EYE_OFF : EYE;
|
||||
eye.setAttribute("aria-pressed", String(show));
|
||||
eye.title = eye.ariaLabel = show ? "Skrýt heslo" : "Zobrazit heslo";
|
||||
pw.focus();
|
||||
});
|
||||
pw.addEventListener("input", () => showError(""));
|
||||
form.querySelector("[data-cancel]").addEventListener("click", () => dlg.close("cancel"));
|
||||
form.addEventListener("submit", async (e) => {
|
||||
if (e.submitter?.value !== "ok") return;
|
||||
e.preventDefault();
|
||||
if (!pw.value) { showError("Zadejte heslo."); pw.focus(); return; }
|
||||
ok.disabled = true;
|
||||
ok.textContent = "Ověřuji…";
|
||||
try {
|
||||
await login(form.pw.value, form.remember.checked);
|
||||
await login(pw.value, form.remember.checked);
|
||||
dlg.close("ok");
|
||||
} catch (ex) {
|
||||
err.textContent = ex.message;
|
||||
err.hidden = false;
|
||||
showError(ex.status === 401 ? "Heslo nesouhlasí, zkuste to znovu." : ex.message);
|
||||
pw.select();
|
||||
pw.focus();
|
||||
} finally {
|
||||
ok.disabled = false;
|
||||
ok.textContent = "Přihlásit";
|
||||
}
|
||||
});
|
||||
dlg.addEventListener("close", () => { dlg.remove(); resolve(dlg.returnValue === "ok"); });
|
||||
dlg.showModal();
|
||||
pw.focus();
|
||||
});
|
||||
}
|
||||
|
||||
const EYE = `<svg viewBox="0 0 24 24" width="20" height="20" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M2 12s3.5-7 10-7 10 7 10 7-3.5 7-10 7S2 12 2 12z"/><circle cx="12" cy="12" r="3"/></svg>`;
|
||||
const EYE_OFF = `<svg viewBox="0 0 24 24" width="20" height="20" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10.6 5.1A10.9 10.9 0 0 1 12 5c6.5 0 10 7 10 7a17.6 17.6 0 0 1-2.9 3.8M6.6 6.6C3.6 8.4 2 12 2 12s3.5 7 10 7c1.8 0 3.4-.5 4.8-1.3"/><path d="M9.9 9.9a3 3 0 0 0 4.2 4.2"/><path d="M3 3l18 18"/></svg>`;
|
||||
|
||||
// Log in/out button for a page's top bar. onChange runs after logging in or out.
|
||||
export async function authButton(container, { uploads, onChange = () => location.reload() }) {
|
||||
if (!uploads) return;
|
||||
// a remembered password may have been changed on the server since
|
||||
if (getPassword()) {
|
||||
try { await login(getPassword(), isRemembered()); }
|
||||
catch (err) { if (err.status === 401) clearPassword(); }
|
||||
}
|
||||
const btn = document.createElement("button");
|
||||
btn.className = "auth";
|
||||
const render = () => { btn.textContent = getPassword() ? "Odhlásit" : "Přihlásit"; };
|
||||
render();
|
||||
btn.addEventListener("click", async () => {
|
||||
if (getPassword()) { clearPassword(); render(); onChange(false); return; }
|
||||
if (await askPassword()) { render(); onChange(true); }
|
||||
});
|
||||
container.append(btn);
|
||||
}
|
||||
|
||||
// Run fn; on 401 ask for the password and retry once.
|
||||
export async function withLogin(fn, reason) {
|
||||
try { return await fn(); }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue