From cd5cb5dd08c92779e8c40613e612ba33fda86c39 Mon Sep 17 00:00:00 2001 From: Bkk1234 Date: Thu, 19 Nov 2026 11:28:20 +0700 Subject: [PATCH] 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 --- AGENTS.md | 9 ++++- assets/css/admin.css | 58 ++++++++++++++++++++++++++++++++ index.html | 2 +- sw.js | 3 +- ui/auth.js | 59 ++++++++++++++++++++++++++++++++ ui/views/admin.js | 80 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 208 insertions(+), 3 deletions(-) create mode 100644 ui/auth.js diff --git a/AGENTS.md b/AGENTS.md index ec33488..47c50bc 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -37,7 +37,8 @@ PUBLISH_PUSH=0 node server/publish.mjs # เขียน+commit แต่ไ | `ui/data.js` | อ่าน/เขียนข้อมูล, normalize, ฉบับร่าง (localStorage), `PUT api/projects` | | `ui/ui.js` | ชุดไอคอน SVG, escape, toast, ตรวจ URL/อีเมล | | `ui/views/home.js` | ฮีโร่ + ค้นหา + การ์ดโปรเจค | -| `ui/views/admin.js` | หลังบ้าน: ฟอร์มแก้ไข, เรียงลำดับ, ลบ, เผยแพร่, ดาวน์โหลด JSON | +| `ui/auth.js` | ด่านชื่อ+รหัสของหลังบ้าน (sessionStorage, ล็อก 30 วิเมื่อผิด 5 ครั้ง) | +| `ui/views/admin.js` | หลังบ้าน: ล็อกอิน → ฟอร์มแก้ไข, เรียงลำดับ, ลบ, เผยแพร่, ดาวน์โหลด JSON | | `server/publish.mjs` | static server + `GET/PUT api/projects` + git commit/push | | `data/projects.json` | ข้อมูลจริง (คีย์เต็มคำ: `project_name`, `project_description`, `project_url`, `contact_email`, `icon`) | @@ -60,6 +61,12 @@ PUBLISH_PUSH=0 node server/publish.mjs # เขียน+commit แต่ไ - `project_url` ต้องมี scheme `http(s)` — เซิร์ฟเวอร์ตอบ 422 ถ้าไม่ผ่าน - `icon` ต้องเป็นคีย์ใน `ICON_LABELS` (`ui/views/admin.js`) ไม่งั้นจะ fallback เป็น `folder` +## หลังบ้านถูกล็อกด้วยชื่อ+รหัส + +`ui/auth.js` เก็บชื่อและรหัสไว้ในไฟล์ (แก้ที่ค่าคงที่ `ADMIN_NAME` / `ADMIN_PASSWORD`) และจำสถานะใน `sessionStorage` → ปิดแท็บแล้วต้องใส่ใหม่ + +**ข้อจำกัดที่ต้องรู้:** ด่านนี้เป็นฝั่งเบราว์เซอร์ ลอกโค้ดไปอ่านได้ จึงกันคนเดินผ่านหน้าจอ ไม่ใช่กันผู้โจมตี — การป้องกันจริงคือ `ADMIN_TOKEN` ของ `server/publish.mjs` ซึ่งต้องตั้งทุกครั้งที่เปิดเซิร์ฟเวอร์ให้เครื่องอื่นเข้าถึง + ## ขั้นตอนตรวจก่อนส่งงาน ```bash diff --git a/assets/css/admin.css b/assets/css/admin.css index 7d995be..d4232bc 100644 --- a/assets/css/admin.css +++ b/assets/css/admin.css @@ -51,6 +51,64 @@ padding: var(--space-5); } +/* ------------------------------------------------------------- auth */ +.auth-wrap { + display: grid; + place-items: center; + min-height: min(72vh, 720px); + padding-block: var(--space-6); +} +.auth-card { + width: min(430px, 100%); + padding: var(--space-8) var(--space-6) var(--space-6); + border: 1px solid var(--border); + border-radius: var(--radius-xl); + background: linear-gradient(180deg, #fff 0%, var(--brand-50) 100%); + box-shadow: var(--shadow-md); + text-align: center; +} +.auth-card__icon { + display: inline-grid; + place-items: center; + width: 52px; + height: 52px; + border-radius: 16px; + background: #fff; + border: 1px solid var(--brand-200); + color: var(--brand-700); + margin-bottom: var(--space-4); +} +.auth-card__icon svg { + width: 26px; + height: 26px; +} +.auth-card__title { + font-size: 1.35rem; +} +.auth-card__sub { + margin-top: 6px; + font-size: 14.5px; + color: var(--muted-foreground); +} +.auth-card form { + margin-top: var(--space-5); + text-align: start; +} +.auth-card .field + .field { + margin-top: var(--space-4); +} +.auth-card .field__error { + margin-top: var(--space-2); + min-height: 0; +} +.auth-card__submit { + width: 100%; + margin-top: var(--space-4); +} +.auth-card .admin-hint { + text-align: center; +} + /* ------------------------------------------------------------- fields */ .field { display: flex; diff --git a/index.html b/index.html index 09e82ad..3532d19 100644 --- a/index.html +++ b/index.html @@ -22,7 +22,7 @@ content="รายชื่อโปรเจคอดิเรกของเรา เลือกโปรเจคที่อยากดูได้เลย" /> - + diff --git a/sw.js b/sw.js index e0af59d..184ae11 100644 --- a/sw.js +++ b/sw.js @@ -2,7 +2,7 @@ กติกา: bump VERSION เฉพาะเมื่อแก้ไฟล์นี้หรือรายการ PRECACHE เนื้อหา (HTML/CSS/JS/JSON) ใช้ network-first จึงไม่ต้อง bump */ -const VERSION = "2026.11.19.2"; +const VERSION = "2026.11.19.3"; const CACHE = `hobbypj-${VERSION}`; const PRECACHE = [ @@ -17,6 +17,7 @@ const PRECACHE = [ "./ui/router.js", "./ui/ui.js", "./ui/data.js", + "./ui/auth.js", "./ui/pwa.js", "./ui/motion.js", "./ui/views/home.js", diff --git a/ui/auth.js b/ui/auth.js new file mode 100644 index 0000000..191c58f --- /dev/null +++ b/ui/auth.js @@ -0,0 +1,59 @@ +/* auth.js — ประตูหน้าหลังบ้าน (ชื่อ + รหัส) + หมายเหตุความปลอดภัย: นี่คือ "ด่านหน้า" ฝั่งเบราว์เซอร์ ทำให้คนเดินผ่านหน้าจอ + ไม่เข้าไปแก้ข้อความได้ง่าย ๆ เท่านั้น ไม่ใช่ระบบยืนยันตัวตนจริง + การป้องกันจริงอยู่ที่ ADMIN_TOKEN ของ server/publish.mjs (ฝั่งเซิร์ฟเวอร์) + ห้ามใช้รหัสเดียวกับระบบอื่น และอย่าเปิดหลังบ้านให้เครื่องอื่นโดยไม่ตั้ง ADMIN_TOKEN */ + +const ADMIN_NAME = "admin"; +const ADMIN_PASSWORD = "admin007"; +const SESSION_KEY = "hobbypj.admin.session.v1"; + +const MAX_ATTEMPTS = 5; +const LOCK_MS = 30_000; + +let failures = 0; +let lockedUntil = 0; + +export function isAuthed() { + try { + return sessionStorage.getItem(SESSION_KEY) === "1"; + } catch { + return false; + } +} + +/** @returns {{ok:boolean, locked?:boolean, retryIn?:number, remaining?:number}} */ +export function login(name, password) { + const now = Date.now(); + if (now < lockedUntil) { + return { ok: false, locked: true, retryIn: Math.ceil((lockedUntil - now) / 1000) }; + } + + const ok = String(name ?? "").trim() === ADMIN_NAME && String(password ?? "") === ADMIN_PASSWORD; + if (!ok) { + failures += 1; + if (failures >= MAX_ATTEMPTS) { + failures = 0; + lockedUntil = now + LOCK_MS; + return { ok: false, locked: true, retryIn: Math.round(LOCK_MS / 1000) }; + } + return { ok: false, remaining: MAX_ATTEMPTS - failures }; + } + + failures = 0; + lockedUntil = 0; + try { + sessionStorage.setItem(SESSION_KEY, "1"); + } catch { + /* โหมดส่วนตัวที่ปิด storage → ให้เข้าได้เฉพาะรอบนี้ */ + } + return { ok: true }; +} + +export function logout() { + try { + sessionStorage.removeItem(SESSION_KEY); + } catch { + /* ignore */ + } +} diff --git a/ui/views/admin.js b/ui/views/admin.js index 03a7e96..e83d950 100644 --- a/ui/views/admin.js +++ b/ui/views/admin.js @@ -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 = ` +
+
+
+ +

เข้าสู่หน้าจัดการ

+

ใส่ชื่อและรหัสก่อนแก้ข้อความบนเว็บ

+
+
+ + +
+
+ + +
+ + +
+

จำไว้เฉพาะแท็บนี้ — ปิดแท็บแล้วต้องใส่ใหม่

+
+
+
`; + + 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")}ดึงข้อมูลล่าสุด ${icon("arrowRight")}ดูหน้าเว็บ + @@ -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/");