mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-20 07:00:58 +08:00
Comments, doc comments, UI labels, log and error messages, LLM prompt strings, markdown docs, test data and assertions. Identifiers, enum keys, routes, placeholders and code structure are unchanged. Notes: - docs/漏洞流量证据.md renamed to docs/vulnerability-traffic-evidence.md - tests that exercised CJK specific handling (GBK zip names, Chinese full text search) now use ASCII equivalents; go build passes and the test suite matches the pre translation baseline (remaining failures need a PostgreSQL DSN and fail identically on the original) - frontend mention syntax now uses English kinds (finding, asset, ...) matching the backend, chat mentions tests pass
47 lines
1.5 KiB
TypeScript
47 lines
1.5 KiB
TypeScript
const TOKEN_KEY = "artex_token";
|
|
const COOKIE_MAX_AGE = 7 * 24 * 60 * 60; // 7 days (seconds)
|
|
|
|
export interface CurrentUser {
|
|
id: string;
|
|
name: string;
|
|
username: string;
|
|
email: string;
|
|
avatar: string;
|
|
role: string;
|
|
}
|
|
|
|
export const auth = {
|
|
getToken(): string | null {
|
|
if (typeof window === "undefined") return null;
|
|
// Mock demo: no real login; return a fake token so the route guard allows going straight into the main UI.
|
|
return localStorage.getItem(TOKEN_KEY) ?? (process.env.NEXT_PUBLIC_MOCK === "1" ? "mock-demo" : null);
|
|
},
|
|
|
|
setToken(token: string): void {
|
|
localStorage.setItem(TOKEN_KEY, token);
|
|
// Also write a cookie for the Next.js middleware to read server-side
|
|
document.cookie = `${TOKEN_KEY}=${encodeURIComponent(token)}; path=/; max-age=${COOKIE_MAX_AGE}; SameSite=Lax`;
|
|
},
|
|
|
|
clearToken(): void {
|
|
localStorage.removeItem(TOKEN_KEY);
|
|
document.cookie = `${TOKEN_KEY}=; path=/; max-age=0`;
|
|
},
|
|
|
|
// Parse the current user from the JWT payload's sub field; display only, no signature verification.
|
|
getCurrentUser(): CurrentUser | null {
|
|
const token = this.getToken();
|
|
if (!token) return null;
|
|
try {
|
|
const parts = token.split(".");
|
|
if (parts.length !== 3) return null;
|
|
// base64url → base64
|
|
const payload = JSON.parse(atob(parts[1].replace(/-/g, "+").replace(/_/g, "/")));
|
|
const username: string = payload.sub ?? "ARTEX";
|
|
return { id: "1", name: username, username, email: "", avatar: "", role: "operator" };
|
|
} catch {
|
|
return null;
|
|
}
|
|
},
|
|
};
|