refactor(layout, i18n, admin): 优化布局结构与多语言支持
调整 AdminShell 组件的子组件顺序,提升代码可读性。更新 admin-breadcrumb 组件,简化导航标签翻译逻辑,确保多语言支持的一致性。重构 admin-language-switcher 组件,优化语言切换的用户体验,增强界面交互性。更新多语言配置,新增登录界面的副标题,提升用户体验。
This commit is contained in:
45
src/middleware.ts
Normal file
45
src/middleware.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { NextResponse, type NextRequest } from "next/server";
|
||||
|
||||
import { ADMIN_TOKEN_STORAGE_KEY } from "@/lib/admin-token-constants";
|
||||
import { readAdminTokenFromCookieString } from "@/lib/admin-token-cookie";
|
||||
|
||||
const ADMIN_LOGIN_PATH = "/admin/login";
|
||||
|
||||
function isAdminLoginPath(pathname: string): boolean {
|
||||
return pathname === ADMIN_LOGIN_PATH || pathname.startsWith(`${ADMIN_LOGIN_PATH}/`);
|
||||
}
|
||||
|
||||
function readTokenFromRequest(request: NextRequest): string | null {
|
||||
const fromCookie = request.cookies.get(ADMIN_TOKEN_STORAGE_KEY)?.value?.trim();
|
||||
if (fromCookie) {
|
||||
return fromCookie;
|
||||
}
|
||||
|
||||
return readAdminTokenFromCookieString(request.headers.get("cookie"));
|
||||
}
|
||||
|
||||
export function middleware(request: NextRequest) {
|
||||
const { pathname } = request.nextUrl;
|
||||
|
||||
if (!pathname.startsWith("/admin")) {
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
if (isAdminLoginPath(pathname)) {
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
const token = readTokenFromRequest(request);
|
||||
if (!token) {
|
||||
const loginUrl = request.nextUrl.clone();
|
||||
loginUrl.pathname = ADMIN_LOGIN_PATH;
|
||||
loginUrl.search = "";
|
||||
return NextResponse.redirect(loginUrl);
|
||||
}
|
||||
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
export const config = {
|
||||
matcher: ["/admin", "/admin/:path*"],
|
||||
};
|
||||
Reference in New Issue
Block a user