Spaces:
Sleeping
Sleeping
Commit ·
d705bd6
1
Parent(s): a23faa4
feat(server): 使用者管理檢視頁 GET /api/admin(排版好的表格)
Browse files- 自帶 token 輸入框的暗色檢視頁,前端 fetch /api/admin/users 帶 Bearer 後渲染成表格
(名稱/來源/建立/遊玩/過關/峰ATK/峰DEF/最高Stage/最後遊玩),可選記住金鑰於本機。
- 同樣只在設了 ADMIN_TOKEN 才註冊;頁面本身為唯讀殼、資料 fetch 仍需金鑰。
- 測試 +涵蓋(/api/admin:未設→404、設了→200 text/html)。
server 測試 76 綠;typecheck/build 通過。
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
packages/server/src/authRoutes.ts
CHANGED
|
@@ -56,6 +56,85 @@ function setCookies(reply: FastifyReply, cookies: string[]): void {
|
|
| 56 |
reply.header("set-cookie", cookies);
|
| 57 |
}
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
export function registerAuthRoutes(app: FastifyInstance, deps: AuthDeps): void {
|
| 60 |
const { userRepo, providers, redirectBase, cookieSecure, devLogin, adminToken } = deps;
|
| 61 |
const authLimit = makeRateLimiter(10); // 登入相關較嚴:每 IP 每分鐘 10 次(用於整頁跳轉登入,走真實用戶 IP���
|
|
@@ -177,6 +256,9 @@ export function registerAuthRoutes(app: FastifyInstance, deps: AuthDeps): void {
|
|
| 177 |
// 5a) 管理唯讀:列出所有使用者。需在請求帶 `Authorization: Bearer <ADMIN_TOKEN>`;
|
| 178 |
// 未設 ADMIN_TOKEN 則整條路由不註冊(預設關閉、不外洩)。不回 providerUserId 等內部識別。
|
| 179 |
if (adminToken) {
|
|
|
|
|
|
|
|
|
|
| 180 |
app.get("/api/admin/users", async (req, reply) => {
|
| 181 |
const provided = parseBearer(req.headers.authorization) ?? "";
|
| 182 |
if (!safeEqual(provided, adminToken)) return reply.code(401).send({ error: "unauthorized" });
|
|
|
|
| 56 |
reply.header("set-cookie", cookies);
|
| 57 |
}
|
| 58 |
|
| 59 |
+
// 唯讀檢視頁(排版好的表格版):自帶 token 輸入框,前端 fetch /api/admin/users 帶 Bearer 後渲染。
|
| 60 |
+
// 純內嵌字串、零相依;內部 JS 一律用字串相加(不用樣板字面值),避免與此 TS 樣板字串衝突。
|
| 61 |
+
const ADMIN_USERS_HTML = `<!doctype html>
|
| 62 |
+
<html lang="zh-Hant"><head>
|
| 63 |
+
<meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
|
| 64 |
+
<title>使用者列表 · Worst Card Game</title>
|
| 65 |
+
<style>
|
| 66 |
+
:root{color-scheme:dark}
|
| 67 |
+
body{margin:0;font-family:"Segoe UI","Microsoft JhengHei",system-ui,sans-serif;background:#0e0f1a;color:#e8e6e0}
|
| 68 |
+
.wrap{max-width:1040px;margin:0 auto;padding:24px 16px}
|
| 69 |
+
h1{color:#d9b25a;font-size:24px;margin:0 0 4px}
|
| 70 |
+
.sub{color:#9a97b0;margin:0 0 16px;font-size:13px}
|
| 71 |
+
.bar{display:flex;gap:8px;flex-wrap:wrap;align-items:center;margin-bottom:12px}
|
| 72 |
+
input[type=password]{background:#1a1c2e;border:1px solid #33365a;color:#e8e6e0;border-radius:8px;padding:8px 12px;font-size:14px;min-width:240px}
|
| 73 |
+
button{background:#6b2230;color:#fff;border:1px solid #33365a;border-radius:8px;padding:8px 18px;font-size:14px;cursor:pointer}
|
| 74 |
+
button:hover{border-color:#d9b25a}
|
| 75 |
+
.msg{color:#f0563a;margin:6px 0;min-height:18px;font-size:14px}
|
| 76 |
+
.count{color:#9a97b0;font-size:14px;margin-bottom:8px}
|
| 77 |
+
table{width:100%;border-collapse:collapse;font-size:14px}
|
| 78 |
+
th,td{text-align:left;padding:8px 10px;border-bottom:1px solid #23253a;white-space:nowrap}
|
| 79 |
+
th{color:#9a97b0;font-weight:600;position:sticky;top:0;background:#0e0f1a}
|
| 80 |
+
td.name{color:#e8e6e0;font-weight:600}
|
| 81 |
+
.prov{font-size:12px;padding:2px 8px;border-radius:999px;background:rgba(255,255,255,.08);color:#9a97b0}
|
| 82 |
+
.num{text-align:right;font-variant-numeric:tabular-nums}
|
| 83 |
+
</style></head><body><div class="wrap">
|
| 84 |
+
<h1>使用者列表</h1>
|
| 85 |
+
<p class="sub">唯讀 · 需管理金鑰。金鑰只留在這台瀏覽器、只用來帶 Authorization 標頭,不會外送他處。</p>
|
| 86 |
+
<div class="bar">
|
| 87 |
+
<input id="tok" type="password" placeholder="ADMIN_TOKEN" autocomplete="off">
|
| 88 |
+
<button id="go">載入</button>
|
| 89 |
+
<label style="color:#9a97b0;font-size:13px"><input id="remember" type="checkbox"> 記住金鑰</label>
|
| 90 |
+
</div>
|
| 91 |
+
<div class="msg" id="msg"></div>
|
| 92 |
+
<div class="count" id="count"></div>
|
| 93 |
+
<table><thead><tr>
|
| 94 |
+
<th>名稱</th><th>來源</th><th>建立</th>
|
| 95 |
+
<th class="num">遊玩</th><th class="num">過關</th>
|
| 96 |
+
<th class="num">峰ATK</th><th class="num">峰DEF</th><th class="num">最高Stage</th>
|
| 97 |
+
<th>最後遊玩</th>
|
| 98 |
+
</tr></thead><tbody id="rows"></tbody></table>
|
| 99 |
+
</div><script>
|
| 100 |
+
(function(){
|
| 101 |
+
var KEY="wcg_admin_token";
|
| 102 |
+
var tok=document.getElementById("tok"), remember=document.getElementById("remember");
|
| 103 |
+
var saved=localStorage.getItem(KEY);
|
| 104 |
+
if(saved){tok.value=saved;remember.checked=true;}
|
| 105 |
+
function fmt(iso){if(!iso)return "—";try{return new Date(iso).toLocaleString();}catch(e){return iso;}}
|
| 106 |
+
function esc(s){var d=document.createElement("span");d.textContent=(s==null?"":String(s));return d.innerHTML;}
|
| 107 |
+
function load(){
|
| 108 |
+
var t=tok.value.trim(), msg=document.getElementById("msg");
|
| 109 |
+
msg.textContent="載入中…";
|
| 110 |
+
if(remember.checked)localStorage.setItem(KEY,t);else localStorage.removeItem(KEY);
|
| 111 |
+
fetch("/api/admin/users",{headers:{Authorization:"Bearer "+t}})
|
| 112 |
+
.then(function(r){if(r.status===401)throw new Error("金鑰錯誤 (401)");if(!r.ok)throw new Error("HTTP "+r.status);return r.json();})
|
| 113 |
+
.then(function(j){
|
| 114 |
+
msg.textContent="";
|
| 115 |
+
document.getElementById("count").textContent="共 "+j.count+" 位";
|
| 116 |
+
var rows=(j.users||[]).map(function(u){
|
| 117 |
+
var s=u.stats||{};
|
| 118 |
+
return "<tr><td class=name>"+esc(u.displayName)+"</td>"+
|
| 119 |
+
"<td><span class=prov>"+esc(u.provider)+"</span></td>"+
|
| 120 |
+
"<td>"+esc(fmt(u.createdAt))+"</td>"+
|
| 121 |
+
"<td class=num>"+(s.plays||0)+"</td>"+
|
| 122 |
+
"<td class=num>"+(s.clears||0)+"</td>"+
|
| 123 |
+
"<td class=num>"+(s.peakAtk||0)+"</td>"+
|
| 124 |
+
"<td class=num>"+(s.peakDef||0)+"</td>"+
|
| 125 |
+
"<td class=num>"+(s.maxReachedStage||0)+"</td>"+
|
| 126 |
+
"<td>"+esc(fmt(s.lastPlayedAt))+"</td></tr>";
|
| 127 |
+
}).join("");
|
| 128 |
+
document.getElementById("rows").innerHTML=rows||"<tr><td colspan=9 style='color:#9a97b0'>(目前沒有使用者)</td></tr>";
|
| 129 |
+
})
|
| 130 |
+
.catch(function(e){msg.textContent=e.message;document.getElementById("rows").innerHTML="";document.getElementById("count").textContent="";});
|
| 131 |
+
}
|
| 132 |
+
document.getElementById("go").addEventListener("click",load);
|
| 133 |
+
tok.addEventListener("keydown",function(e){if(e.key==="Enter")load();});
|
| 134 |
+
if(saved)load();
|
| 135 |
+
})();
|
| 136 |
+
</script></body></html>`;
|
| 137 |
+
|
| 138 |
export function registerAuthRoutes(app: FastifyInstance, deps: AuthDeps): void {
|
| 139 |
const { userRepo, providers, redirectBase, cookieSecure, devLogin, adminToken } = deps;
|
| 140 |
const authLimit = makeRateLimiter(10); // 登入相關較嚴:每 IP 每分鐘 10 次(用於整頁跳轉登入,走真實用戶 IP���
|
|
|
|
| 256 |
// 5a) 管理唯讀:列出所有使用者。需在請求帶 `Authorization: Bearer <ADMIN_TOKEN>`;
|
| 257 |
// 未設 ADMIN_TOKEN 則整條路由不註冊(預設關閉、不外洩)。不回 providerUserId 等內部識別。
|
| 258 |
if (adminToken) {
|
| 259 |
+
// 排版好的檢視頁:瀏覽器直接開 /api/admin、貼金鑰即看(資料 fetch 仍需 Bearer)。
|
| 260 |
+
app.get("/api/admin", async (_req, reply) => reply.type("text/html; charset=utf-8").send(ADMIN_USERS_HTML));
|
| 261 |
+
|
| 262 |
app.get("/api/admin/users", async (req, reply) => {
|
| 263 |
const provided = parseBearer(req.headers.authorization) ?? "";
|
| 264 |
if (!safeEqual(provided, adminToken)) return reply.code(401).send({ error: "unauthorized" });
|
packages/server/test/auth.test.ts
CHANGED
|
@@ -95,9 +95,10 @@ describe("帳號 / 統計路由(dev-login e2e)", () => {
|
|
| 95 |
expect((await app.inject({ method: "GET", url: "/api/me" })).statusCode).toBe(404);
|
| 96 |
});
|
| 97 |
|
| 98 |
-
it("未設 ADMIN_TOKEN → /api/admin/users 不註冊(404)", async () => {
|
| 99 |
const { app } = makeApp(); // 無 adminToken
|
| 100 |
expect((await app.inject({ method: "GET", url: "/api/admin/users" })).statusCode).toBe(404);
|
|
|
|
| 101 |
});
|
| 102 |
|
| 103 |
it("ADMIN_TOKEN 保護:無/錯 token → 401,正確 token → 列出使用者且不洩 providerUserId", async () => {
|
|
@@ -108,6 +109,11 @@ describe("帳號 / 統計路由(dev-login e2e)", () => {
|
|
| 108 |
expect((await app.inject({ method: "GET", url: "/api/admin/users" })).statusCode).toBe(401); // 無 token
|
| 109 |
expect((await app.inject({ method: "GET", url: "/api/admin/users", headers: { authorization: "Bearer wrong" } })).statusCode).toBe(401);
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
const ok = await app.inject({ method: "GET", url: "/api/admin/users", headers: { authorization: "Bearer s3cret-admin" } });
|
| 112 |
expect(ok.statusCode).toBe(200);
|
| 113 |
expect(ok.json().count).toBe(2);
|
|
|
|
| 95 |
expect((await app.inject({ method: "GET", url: "/api/me" })).statusCode).toBe(404);
|
| 96 |
});
|
| 97 |
|
| 98 |
+
it("未設 ADMIN_TOKEN → /api/admin 與 /api/admin/users 都不註冊(404)", async () => {
|
| 99 |
const { app } = makeApp(); // 無 adminToken
|
| 100 |
expect((await app.inject({ method: "GET", url: "/api/admin/users" })).statusCode).toBe(404);
|
| 101 |
+
expect((await app.inject({ method: "GET", url: "/api/admin" })).statusCode).toBe(404);
|
| 102 |
});
|
| 103 |
|
| 104 |
it("ADMIN_TOKEN 保護:無/錯 token → 401,正確 token → 列出使用者且不洩 providerUserId", async () => {
|
|
|
|
| 109 |
expect((await app.inject({ method: "GET", url: "/api/admin/users" })).statusCode).toBe(401); // 無 token
|
| 110 |
expect((await app.inject({ method: "GET", url: "/api/admin/users", headers: { authorization: "Bearer wrong" } })).statusCode).toBe(401);
|
| 111 |
|
| 112 |
+
// 檢視頁本身不需 token(只是殼,資料 fetch 才要)
|
| 113 |
+
const page = await app.inject({ method: "GET", url: "/api/admin" });
|
| 114 |
+
expect(page.statusCode).toBe(200);
|
| 115 |
+
expect(page.headers["content-type"]).toContain("text/html");
|
| 116 |
+
|
| 117 |
const ok = await app.inject({ method: "GET", url: "/api/admin/users", headers: { authorization: "Bearer s3cret-admin" } });
|
| 118 |
expect(ok.statusCode).toBe(200);
|
| 119 |
expect(ok.json().count).toBe(2);
|