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
+59
@@ -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 */
|
||||
}
|
||||
}
|
||||
@@ -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