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:
Bkk1234 committed 2026-11-19 11:28:20 +07:00
1 parent 79b147d38f
commit cd5cb5dd08
6 files changed
+208 -3

No files matched your search

+8 -1
View File
@@ -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
+58
View File
@@ -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;
+1 -1
View File
@@ -22,7 +22,7 @@
content="รายชื่อโปรเจคอดิเรกของเรา เลือกโปรเจคที่อยากดูได้เลย"
/>
<meta name="theme-color" content="#8A4722" />
<meta name="app-version" content="2026.11.19.2" />
<meta name="app-version" content="2026.11.19.3" />
<link rel="manifest" href="manifest.json" />
<link rel="icon" type="image/png" sizes="192x192" href="icons/icon-192.png" />
<link rel="icon" type="image/png" sizes="64x64" href="icons/favicon-64.png" />
+2 -1
View File
@@ -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",
+59
View File
@@ -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 */
}
}
+80
View File
@@ -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/");