refactor(auth): consolidate auth under /api/auth/* endpoints
Some checks failed
Deploy eCourt Note Web to Server / Validate Database Schema (Drizzle) (push) Successful in 3s
Deploy eCourt Note Web to Server / Build Web Docker Image (push) Failing after 30s
Deploy eCourt Note Web to Server / Deploy eCourt Note Web to Server (push) Has been skipped
Deploy eCourt Note Web to Server / Notify Deployment Status (push) Failing after 1s
Some checks failed
Deploy eCourt Note Web to Server / Validate Database Schema (Drizzle) (push) Successful in 3s
Deploy eCourt Note Web to Server / Build Web Docker Image (push) Failing after 30s
Deploy eCourt Note Web to Server / Deploy eCourt Note Web to Server (push) Has been skipped
Deploy eCourt Note Web to Server / Notify Deployment Status (push) Failing after 1s
- ยุบ route auth ที่เคย copy กันจน drift (flow N11 หายจาก /api/auth/session) เหลือโค้ด inline สองเส้นเดียว: POST /api/auth/token (ทางเข้า 3: x-api-key docver / x-api-key purpose:"login" mint ลิงก์ note / ticket ของ add-in) + POST /api/auth/session (แลก credential + register N8 + N11 fid-only) - POST /api/documents/token คงไว้เป็น alias re-export (กลุ่ม docver/e-Court เดิม — contract เดิมเป๊ะ, มี test toBe กัน copy แยกชุดกลับมา) - ถอดเส้นฝั่ง note ทั้งสอบ: /api/note/auth/token + /api/note/auth/session (e-Court ต้องย้ายมาทางเข้ารวม — แจ้งทีม e-Court ก่อน deploy) - utils/auth-store.ts: primitives กลางฝั่ง client (jwtExp/isExpired/ localStorage store + memory fallback) — lib/note-session ใช้ร่วม (expMs decode จาก JWT payload = นาฬิกา server) และวางไว้ให้ wordaddin ย้ายมาใช้รอบหน้าได้ (รอบนี้ไม่แต้ wordaddin ตามข้อตกลง) - นโยบาย token หมดอายุกลาง session: PDFViewer reportActionError จับ auth-error ของ Server Action ทุกจุด → ล้าง session แล้วพาไป /note?sso=expired (จบที่ gate เดียวกับ bootstrap 401) - openapi: ทางเข้า purpose:"login" + schema LoginTokenRequest + ลบ path เส้นที่ถอด + test กันเส้นตายหลุดค้างใน spec - เอกสาร sync: PLAN-SSO §0.6, CLAUDE.md, PLAN-STAGED, .env.example, /note/dev - tests: ย้าย/เพิ่มชุด auth (50 เคสกลุ่ม auth ผ่าน, ทั้งชุด 444 ผ่าน) + e2e ครบทั้ง API (mint/alias/ticket/404 เส้นเก่า/N11/D4) และ browser Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
parent
c57a75e36a
commit
5121970f31
26 changed files with 625 additions and 523 deletions
|
|
@ -7,7 +7,8 @@ import { withBasePath } from "@/utils/base-path";
|
|||
/**
|
||||
* ฟอร์ม "จำลอง e-Court" บนหน้า /note/dev สำหรับตอน mint อัตโนมัติปิด
|
||||
* (production เสมอตาม devMintEnabled) — ผู้ทดสอบใส่ DOCVER_API_KEY แล้วยิง
|
||||
* POST /api/note/auth/token (เส้นเดียวกับที่ backend e-Court ใช้จริง) ได้
|
||||
* POST /api/auth/token แบบ purpose:"login" (ทางเข้ารวม 2026-11-09 — เส้นเดียว
|
||||
* กับที่ backend e-Court ใช้จริง) ได้
|
||||
* access_token มาแล้วพาเข้า /note?fileUrl=…&t=… ให้ NoteHome แลกเป็น session
|
||||
* ต่อ — ไม่เป็นประตูหลังเพราะต้องถือ key ที่ถูกต้องอยู่แล้ว
|
||||
* (คำขอผู้ใช้ 2026-11-02: กดเข้าเอกสารจากหน้า /note/dev บน production ให้ได้)
|
||||
|
|
@ -25,16 +26,18 @@ export default function MockEcourtForm({ files }: { files: string[] }) {
|
|||
setBusy(true);
|
||||
setError(null);
|
||||
try {
|
||||
const res = await fetch(withBasePath("/api/note/auth/token"), {
|
||||
const res = await fetch(withBasePath("/api/auth/token"), {
|
||||
method: "POST",
|
||||
headers: { "x-api-key": apiKey, "content-type": "application/json" },
|
||||
// fid ผูกไฟล์ที่เลือกแบบเดียวกับ dev-link (dev-<file>) — แต่ละไฟล์
|
||||
// ตัวอย่างมี row ecourt: ของตัวเอง ไม่ปนกัน
|
||||
// ตัวอย่างมี row ecourt: ของตัวเอง ไม่ปนกัน · purpose:"login" = ทางเข้า
|
||||
// mint login-token ของ /api/auth/token (ไม่ส่ง = docver-token)
|
||||
body: JSON.stringify({
|
||||
userid: userid.trim(),
|
||||
username: username.trim(),
|
||||
fid: `dev-${file}`,
|
||||
ttl_seconds: 600,
|
||||
purpose: "login",
|
||||
}),
|
||||
});
|
||||
const data = (await res.json().catch(() => null)) as
|
||||
|
|
|
|||
|
|
@ -8,7 +8,8 @@ import { withBasePath } from "@/utils/base-path";
|
|||
* ฟอร์ม "จำลอง e-Court — ส่งเนื้อไฟล์เข้า staging" บนหน้า /note/dev (N11 —
|
||||
* PLAN-STAGED.md · คำขอผู้ใช้ 2026-11-04): กดทดสอบ contract ทั้ง 3 ขั้นตามที่
|
||||
* backend e-Court ต้องทำจริงในหน้าเดียว — (1) POST /api/note/files/stage
|
||||
* (base64 ของไฟล์ที่เลือก) → (2) POST /api/note/auth/token ด้วย fid เดิม →
|
||||
* (base64 ของไฟล์ที่เลือก) → (2) POST /api/auth/token แบบ purpose:"login"
|
||||
* ด้วย fid เดิม (ทางเข้ารวม 2026-11-09) →
|
||||
* (3) ลิงก์ /note?t=… (fid-only ไม่มี fileUrl — D5)
|
||||
* · ต้องใส่ DOCVER_API_KEY จริงทุกครั้งจึงไม่เป็นประตูหลัง (เหมือน MockEcourtForm)
|
||||
* · หน้า /note/dev เปิดด้วย env NOTE_SSO_DEV_MINT=1 อย่างเดียว
|
||||
|
|
@ -71,7 +72,8 @@ export default function MockStageForm() {
|
|||
}
|
||||
|
||||
// (2) mint login token ด้วย fid **เดิม** — dedupe key ecourt:<fid> ผูกกัน
|
||||
const tokenRes = await fetch(withBasePath("/api/note/auth/token"), {
|
||||
// (purpose:"login" = ทางเข้า mint login-token ของ /api/auth/token)
|
||||
const tokenRes = await fetch(withBasePath("/api/auth/token"), {
|
||||
method: "POST",
|
||||
headers: { "x-api-key": apiKey, "content-type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
|
|
@ -79,6 +81,7 @@ export default function MockStageForm() {
|
|||
username: username.trim(),
|
||||
fid: fid.trim(),
|
||||
ttl_seconds: 600,
|
||||
purpose: "login",
|
||||
}),
|
||||
});
|
||||
const tokenData = (await tokenRes.json().catch(() => null)) as
|
||||
|
|
|
|||
|
|
@ -3,7 +3,8 @@
|
|||
// หน้าแรก /note — ข้อมูลเกี่ยวกับระบบ (2026-10-30 ตามคำขอผู้ใช้: ระบบทำอะไรได้ /
|
||||
// วิธีเข้าใช้) + bootstrap ของลิงก์ e-Court (contract ปลายทางลิงก์ยังชี้ที่นี่):
|
||||
// มี credential ใน URL (t หรือ ticket_data+HmacSha256 — จากลิงก์ที่ e-Court ชี้ตรงมา
|
||||
// ที่ /note) = แลกเป็น access_token ที่ POST /api/note/auth/session แล้วเก็บ
|
||||
// ที่ /note) = แลกเป็น access_token ที่ POST /api/auth/session (ทางเข้ารวม
|
||||
// 2026-11-09 — ย้ายมาเส้นสั้นจาก /api/note/auth/session) แล้วเก็บ
|
||||
// localStorage "note-auth" (แบบ add-in) → ตัด credential ออกจาก URL ทันที → มี fileId
|
||||
// = พาเข้า viewer / ไม่มี = แสดงหน้าข้อมูล · ไม่มี credential = หน้า public ธรรมดา
|
||||
// (แบนเนอร์ ?sso= ของลิงก์เก่า + fileError ยังรองรับ)
|
||||
|
|
@ -20,7 +21,7 @@ import {
|
|||
|
||||
import AppNav from "@/components/AppNav";
|
||||
import { parseSsoLinkParams, ssoBanner, type SsoBanner } from "@/lib/note-auth";
|
||||
import { saveNoteSession } from "@/lib/note-session";
|
||||
import { noteExpMs, saveNoteSession } from "@/lib/note-session";
|
||||
import { withBasePath } from "@/utils/base-path";
|
||||
|
||||
type Phase = "boot" | "exchanging" | "ready";
|
||||
|
|
@ -80,7 +81,7 @@ export default function NoteHome() {
|
|||
const timeoutId = window.setTimeout(() => controller.abort(), 45_000);
|
||||
void (async () => {
|
||||
try {
|
||||
const res = await fetch(withBasePath("/api/note/auth/session"), {
|
||||
const res = await fetch(withBasePath("/api/auth/session"), {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify(body),
|
||||
|
|
@ -110,7 +111,9 @@ export default function NoteHome() {
|
|||
token: access_token,
|
||||
userid,
|
||||
username,
|
||||
expMs: Date.now() + expires_in * 1000,
|
||||
// exp จาก JWT payload (นาฬิกา server) แม่นกว่า expires_in — fallback
|
||||
// expires_in เมื่อ decode ไม่ได้
|
||||
expMs: noteExpMs(access_token, expires_in),
|
||||
});
|
||||
if (fileError !== undefined) setFileError(fileError);
|
||||
if (fileId !== undefined) {
|
||||
|
|
|
|||
|
|
@ -58,6 +58,7 @@ import {
|
|||
useAnnotationRealtime,
|
||||
} from "@/hooks/useAnnotationRealtime";
|
||||
import { useHistory } from "@/hooks/useHistory";
|
||||
import { clearNoteSession, isNoteAuthExpiredError } from "@/lib/note-session";
|
||||
import {
|
||||
useCustomSelection,
|
||||
type CustomSelectionCommit,
|
||||
|
|
@ -2216,6 +2217,22 @@ export default function PDFViewer({
|
|||
}, 6000);
|
||||
}
|
||||
|
||||
/** ท้ายร่วมของ catch ของ Server Action ทุกตัว (2026-11-09): ล้มเพราะ token หมด
|
||||
* อายุ/ไม่ผ่าน (message sentinel ของ requireUser) = ล้าง session แล้วพากลับหน้า
|
||||
* แรก ?sso=expired — token ที่ตายแล้วไม่มีทางฟื้น (browser ไม่มี ticket ให้ renew
|
||||
* แบบ add-in) โชว์ toast ต่อก็กดอะไรไม่ได้อีก สู้จบที่ gate เดียวกับตอน bootstrap
|
||||
* 401 ของ AnnotateView · คืน true เมื่อจัดการแล้ว กรณีอื่น log (เดิม) แล้วคืน false
|
||||
* ให้ caller โชว์ error ของตัวเองต่อ */
|
||||
function reportActionError(err: unknown, logLabel: string): boolean {
|
||||
console.error(logLabel, err);
|
||||
if (!isNoteAuthExpiredError(err)) return false;
|
||||
clearNoteSession();
|
||||
// withBasePath("") = /docedit/note — แบนเนอร์ ?sso=expired ของ NoteHome ต่อ (D11:
|
||||
// ห้ามส่ง "/note" เข้า withBasePath)
|
||||
window.location.replace(`${withBasePath("")}?sso=expired`);
|
||||
return true;
|
||||
}
|
||||
|
||||
async function handleCreateAnnotation(
|
||||
pageIndex: number,
|
||||
type: AnnotationType,
|
||||
|
|
@ -2289,8 +2306,10 @@ export default function PDFViewer({
|
|||
},
|
||||
});
|
||||
} catch (err) {
|
||||
console.error("บันทึก annotation ไม่สำเร็จ", err); // React rollback optimistic row ให้เอง
|
||||
showActionError("บันทึกรายการไม่สำเร็จ — ตรวจสอบการเชื่อมต่อแล้วลองอีกครั้ง");
|
||||
// React rollback optimistic row ให้เอง
|
||||
if (!reportActionError(err, "บันทึก annotation ไม่สำเร็จ")) {
|
||||
showActionError("บันทึกรายการไม่สำเร็จ — ตรวจสอบการเชื่อมต่อแล้วลองอีกครั้ง");
|
||||
}
|
||||
} finally {
|
||||
pendingCreatesRef.current.delete(temp.id);
|
||||
}
|
||||
|
|
@ -2342,8 +2361,9 @@ export default function PDFViewer({
|
|||
});
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("ลบ annotation ไม่สำเร็จ", err);
|
||||
showActionError("ลบรายการไม่สำเร็จ — ตรวจสอบการเชื่อมต่อแล้วลองอีกครั้ง");
|
||||
if (!reportActionError(err, "ลบ annotation ไม่สำเร็จ")) {
|
||||
showActionError("ลบรายการไม่สำเร็จ — ตรวจสอบการเชื่อมต่อแล้วลองอีกครั้ง");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -2386,8 +2406,10 @@ export default function PDFViewer({
|
|||
},
|
||||
});
|
||||
} catch (err) {
|
||||
console.error("แก้ไข annotation ไม่สำเร็จ", err); // React rollback optimistic row ให้เอง
|
||||
showActionError("แก้ไขรายการไม่สำเร็จ — ตรวจสอบการเชื่อมต่อแล้วลองอีกครั้ง");
|
||||
// React rollback optimistic row ให้เอง
|
||||
if (!reportActionError(err, "แก้ไข annotation ไม่สำเร็จ")) {
|
||||
showActionError("แก้ไขรายการไม่สำเร็จ — ตรวจสอบการเชื่อมต่อแล้วลองอีกครั้ง");
|
||||
}
|
||||
// ทิ้ง base ที่คิวเก็บไว้ — เป็น data ที่ action นี้ตั้งใจเขียนแต่ไม่เคยติด DB
|
||||
// ถ้าคงไว้ update ถัดไปจะผสานจน commit การเปลี่ยนที่ล้มไปด้วย และ undo
|
||||
// history กู้สถานะที่ไม่มีอยู่จริง (code review 2026-09-29) — รอบหน้าเริ่ม
|
||||
|
|
@ -2458,8 +2480,10 @@ export default function PDFViewer({
|
|||
const saved = await setAnnotationResolved(token, id, resolved);
|
||||
setAnnotations((prev) => prev.map((a) => (a.id === saved.id ? saved : a)));
|
||||
} catch (err) {
|
||||
console.error("ตั้งสถานะไม่สำเร็จ", err); // React rollback optimistic ให้เอง
|
||||
showActionError("ตั้งสถานะไม่สำเร็จ — ตรวจสอบการเชื่อมต่อแล้วลองอีกครั้ง");
|
||||
// React rollback optimistic ให้เอง
|
||||
if (!reportActionError(err, "ตั้งสถานะไม่สำเร็จ")) {
|
||||
showActionError("ตั้งสถานะไม่สำเร็จ — ตรวจสอบการเชื่อมต่อแล้วลองอีกครั้ง");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -2481,8 +2505,9 @@ export default function PDFViewer({
|
|||
);
|
||||
return true;
|
||||
} catch (err) {
|
||||
console.error("ตอบ comment ไม่สำเร็จ", err);
|
||||
showActionError("ตอบ comment ไม่สำเร็จ — ตรวจสอบการเชื่อมต่อแล้วลองอีกครั้ง");
|
||||
if (!reportActionError(err, "ตอบ comment ไม่สำเร็จ")) {
|
||||
showActionError("ตอบ comment ไม่สำเร็จ — ตรวจสอบการเชื่อมต่อแล้วลองอีกครั้ง");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
@ -2493,8 +2518,9 @@ export default function PDFViewer({
|
|||
await deleteReply(token, id);
|
||||
setReplies((prev) => prev.filter((r) => r.id !== id));
|
||||
} catch (err) {
|
||||
console.error("ลบคำตอบไม่สำเร็จ", err);
|
||||
showActionError("ลบคำตอบไม่สำเร็จ — ตรวจสอบการเชื่อมต่อแล้วลองอีกครั้ง");
|
||||
if (!reportActionError(err, "ลบคำตอบไม่สำเร็จ")) {
|
||||
showActionError("ลบคำตอบไม่สำเร็จ — ตรวจสอบการเชื่อมต่อแล้วลองอีกครั้ง");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -2650,8 +2676,9 @@ export default function PDFViewer({
|
|||
);
|
||||
flashBookmarkSaved();
|
||||
} catch (err) {
|
||||
console.error("บันทึก bookmark ไม่สำเร็จ", err);
|
||||
setBookmarkError(err instanceof Error ? err.message : "บันทึกไม่สำเร็จ ลองอีกครั้ง");
|
||||
if (!reportActionError(err, "บันทึก bookmark ไม่สำเร็จ")) {
|
||||
setBookmarkError(err instanceof Error ? err.message : "บันทึกไม่สำเร็จ ลองอีกครั้ง");
|
||||
}
|
||||
} finally {
|
||||
bookmarkPendingRef.current = false;
|
||||
setBookmarkSaving(false);
|
||||
|
|
@ -2676,7 +2703,7 @@ export default function PDFViewer({
|
|||
prev.some((b) => b.id === saved.id) ? prev : [...prev, saved],
|
||||
);
|
||||
} catch (err) {
|
||||
console.error("เพิ่ม sub-bookmark ไม่สำเร็จ", err);
|
||||
reportActionError(err, "เพิ่ม sub-bookmark ไม่สำเร็จ");
|
||||
} finally {
|
||||
bookmarkPendingRef.current = false;
|
||||
}
|
||||
|
|
@ -2692,7 +2719,7 @@ export default function PDFViewer({
|
|||
const saved = await updateBookmark(token, { id, ...patch });
|
||||
setBookmarks((prev) => prev.map((b) => (b.id === saved.id ? saved : b)));
|
||||
} catch (err) {
|
||||
console.error("แก้ไข bookmark ไม่สำเร็จ", err);
|
||||
reportActionError(err, "แก้ไข bookmark ไม่สำเร็จ");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -2704,7 +2731,7 @@ export default function PDFViewer({
|
|||
await deleteBookmark(token, id);
|
||||
setBookmarks((prev) => prev.filter((b) => b.id !== id));
|
||||
} catch (err) {
|
||||
console.error("ลบ bookmark ไม่สำเร็จ", err);
|
||||
reportActionError(err, "ลบ bookmark ไม่สำเร็จ");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue