feat(admin): gate the backoffice behind a name and password
- ui/auth.js: name/password check, session kept in sessionStorage (new tab asks again), 5 wrong attempts lock the form for 30s - admin view renders a login card until authenticated; adds an explicit logout button - SW precache includes auth.js, version bumped to 2026.11.19.3 (meta + sw.js in sync) - docs: AGENTS.md notes the gate is client-side; real protection is the server ADMIN_TOKEN
This commit is contained in:
1 parent
79b147d38f
commit
cd5cb5dd08
6 files changed
+208
-3
No files matched your search
@@ -2,6 +2,7 @@
|
||||
|
||||
import { escapeHtml, icon, isEmail, normalizeUrl, toast, debounce } from "../ui.js";
|
||||
import { initReveal } from "../motion.js";
|
||||
import { isAuthed, login, logout } from "../auth.js";
|
||||
import {
|
||||
clearDraft,
|
||||
createProject,
|
||||
@@ -57,8 +58,78 @@ export function adminView(ctx) {
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------- render */
|
||||
function renderLogin() {
|
||||
root.innerHTML = `
|
||||
<div class="wrap">
|
||||
<div class="auth-wrap">
|
||||
<section class="auth-card" data-reveal="pop" aria-labelledby="auth-title">
|
||||
<span class="auth-card__icon" aria-hidden="true">${icon("shield")}</span>
|
||||
<h1 class="auth-card__title" id="auth-title">เข้าสู่หน้าจัดการ</h1>
|
||||
<p class="auth-card__sub">ใส่ชื่อและรหัสก่อนแก้ข้อความบนเว็บ</p>
|
||||
<form data-login novalidate autocomplete="on">
|
||||
<div class="field">
|
||||
<label class="field__label" for="login-name">ชื่อ</label>
|
||||
<input id="login-name" name="username" type="text" autocomplete="username"
|
||||
autocapitalize="none" autocorrect="off" spellcheck="false" required />
|
||||
</div>
|
||||
<div class="field">
|
||||
<label class="field__label" for="login-password">รหัส</label>
|
||||
<input id="login-password" name="password" type="password"
|
||||
autocomplete="current-password" required />
|
||||
</div>
|
||||
<p class="field__error" data-login-error role="alert"></p>
|
||||
<button class="btn btn--primary auth-card__submit" type="submit">
|
||||
${icon("arrowRight")}<span>เข้าสู่ระบบ</span>
|
||||
</button>
|
||||
</form>
|
||||
<p class="admin-hint">จำไว้เฉพาะแท็บนี้ — ปิดแท็บแล้วต้องใส่ใหม่</p>
|
||||
</section>
|
||||
</div>
|
||||
</div>`;
|
||||
|
||||
initReveal(root);
|
||||
|
||||
const form = root.querySelector("[data-login]");
|
||||
const errorEl = root.querySelector("[data-login-error]");
|
||||
const nameEl = root.querySelector("#login-name");
|
||||
const passEl = root.querySelector("#login-password");
|
||||
const submitEl = root.querySelector(".auth-card__submit");
|
||||
|
||||
root.querySelector("#login-name")?.focus({ preventScroll: true });
|
||||
|
||||
form.addEventListener("submit", (event) => {
|
||||
event.preventDefault();
|
||||
errorEl.textContent = "";
|
||||
[nameEl, passEl].forEach((field) => field.closest(".field").classList.remove("field--error"));
|
||||
if (!nameEl.value.trim() || !passEl.value) {
|
||||
errorEl.textContent = "ใส่ทั้งชื่อและรหัสก่อน";
|
||||
(nameEl.value.trim() ? passEl : nameEl).focus();
|
||||
return;
|
||||
}
|
||||
submitEl.disabled = true;
|
||||
const result = login(nameEl.value, passEl.value);
|
||||
submitEl.disabled = false;
|
||||
if (result.ok) {
|
||||
mount(root);
|
||||
toast("เข้าสู่หน้าจัดการแล้ว");
|
||||
return;
|
||||
}
|
||||
const message = result.locked
|
||||
? `ลองผิดหลายครั้งเกินไป — รออีก ${result.retryIn} วินาที`
|
||||
: `ชื่อหรือรหัสไม่ถูกต้อง (ลองได้อีก ${result.remaining} ครั้ง)`;
|
||||
errorEl.textContent = message;
|
||||
passEl.value = "";
|
||||
[nameEl, passEl].forEach((field) => field.closest(".field").classList.add("field--error"));
|
||||
passEl.focus();
|
||||
});
|
||||
}
|
||||
|
||||
function mount(container) {
|
||||
root = container;
|
||||
if (!isAuthed()) {
|
||||
renderLogin();
|
||||
return;
|
||||
}
|
||||
published = normalizeData(ctx.data);
|
||||
const stored = readDraft();
|
||||
draft = stored && serialize(stored) !== serialize(published) ? stored : normalizeData(published);
|
||||
@@ -79,6 +150,9 @@ export function adminView(ctx) {
|
||||
${icon("refresh")}<span>ดึงข้อมูลล่าสุด</span>
|
||||
</button>
|
||||
<a class="btn btn--ghost" href="#/">${icon("arrowRight")}<span>ดูหน้าเว็บ</span></a>
|
||||
<button class="btn btn--ghost" type="button" data-act="logout">
|
||||
${icon("close")}<span>ออกจากระบบ</span>
|
||||
</button>
|
||||
<button class="btn btn--primary" type="button" data-act="add">
|
||||
${icon("plus")}<span>เพิ่มโปรเจค</span>
|
||||
</button>
|
||||
@@ -214,6 +288,12 @@ export function adminView(ctx) {
|
||||
reloadFromServer();
|
||||
return;
|
||||
}
|
||||
if (act === "logout") {
|
||||
logout();
|
||||
mount(root);
|
||||
toast("ออกจากระบบแล้ว");
|
||||
return;
|
||||
}
|
||||
if (act === "download") {
|
||||
downloadJson(draft);
|
||||
toast("ดาวน์โหลด projects.json แล้ว — เอาไปวางในโฟลเดอร์ data/");
|
||||
|
||||
Reference in new issue
Block a user