26 lines
1 KiB
TypeScript
26 lines
1 KiB
TypeScript
|
|
// anonymous identity เก็บใน localStorage สร้างครั้งเดียวต่อ browser (PLAN.md §0 A4)
|
||
|
|
// ใช้เติม field userId/userName ในตาราง annotations/bookmarks — ไม่มีระบบ auth
|
||
|
|
|
||
|
|
export type LocalUser = { userId: string; userName: string };
|
||
|
|
|
||
|
|
const STORAGE_KEY = "judes-note:user";
|
||
|
|
|
||
|
|
export function getLocalUser(): LocalUser {
|
||
|
|
const raw = localStorage.getItem(STORAGE_KEY);
|
||
|
|
if (raw) {
|
||
|
|
try {
|
||
|
|
const parsed = JSON.parse(raw) as LocalUser;
|
||
|
|
if (typeof parsed.userId === "string" && typeof parsed.userName === "string") {
|
||
|
|
return parsed;
|
||
|
|
}
|
||
|
|
} catch {
|
||
|
|
// ข้อมูลเก่าเสียหาย → สร้างใหม่ต่อด้านล่าง
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
const userId = crypto.randomUUID();
|
||
|
|
const userName = `ผู้ใช้ ${userId.slice(0, 4).toUpperCase()}`;
|
||
|
|
localStorage.setItem(STORAGE_KEY, JSON.stringify({ userId, userName }));
|
||
|
|
return { userId, userName };
|
||
|
|
}
|