refactor(layout, i18n, admin): 优化布局结构与多语言支持
调整 AdminShell 组件的子组件顺序,提升代码可读性。更新 admin-breadcrumb 组件,简化导航标签翻译逻辑,确保多语言支持的一致性。重构 admin-language-switcher 组件,优化语言切换的用户体验,增强界面交互性。更新多语言配置,新增登录界面的副标题,提升用户体验。
This commit is contained in:
61
src/lib/admin-token-cookie.ts
Normal file
61
src/lib/admin-token-cookie.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import {
|
||||
ADMIN_TOKEN_COOKIE_MAX_AGE_SECONDS,
|
||||
ADMIN_TOKEN_STORAGE_KEY,
|
||||
} from "@/lib/admin-token-constants";
|
||||
|
||||
export function readAdminTokenFromCookieString(
|
||||
cookieHeader: string | null | undefined,
|
||||
): string | null {
|
||||
if (!cookieHeader) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const prefix = `${ADMIN_TOKEN_STORAGE_KEY}=`;
|
||||
for (const part of cookieHeader.split(";")) {
|
||||
const trimmed = part.trim();
|
||||
if (!trimmed.startsWith(prefix)) {
|
||||
continue;
|
||||
}
|
||||
const raw = trimmed.slice(prefix.length);
|
||||
if (!raw) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
const decoded = decodeURIComponent(raw).trim();
|
||||
return decoded !== "" ? decoded : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export function readAdminTokenFromDocumentCookie(): string | null {
|
||||
if (typeof document === "undefined") {
|
||||
return null;
|
||||
}
|
||||
|
||||
return readAdminTokenFromCookieString(document.cookie);
|
||||
}
|
||||
|
||||
export function writeAdminTokenCookie(token: string | null): void {
|
||||
if (typeof document === "undefined") {
|
||||
return;
|
||||
}
|
||||
|
||||
const secure =
|
||||
typeof window !== "undefined" && window.location.protocol === "https:";
|
||||
const base = `path=/; SameSite=Lax`;
|
||||
|
||||
if (!token || token.trim() === "") {
|
||||
document.cookie = `${ADMIN_TOKEN_STORAGE_KEY}=; ${base}; max-age=0`;
|
||||
return;
|
||||
}
|
||||
|
||||
const value = encodeURIComponent(token.trim());
|
||||
const maxAge = `max-age=${ADMIN_TOKEN_COOKIE_MAX_AGE_SECONDS}`;
|
||||
document.cookie = `${ADMIN_TOKEN_STORAGE_KEY}=${value}; ${base}; ${maxAge}${
|
||||
secure ? "; Secure" : ""
|
||||
}`;
|
||||
}
|
||||
Reference in New Issue
Block a user