refactor: 替换管理员登录组件并更新会话管理

This commit is contained in:
2026-05-09 13:48:11 +08:00
parent cda7824eb2
commit 9805f56d3a
15 changed files with 359 additions and 77 deletions

View File

@@ -0,0 +1,40 @@
"use client";
import { useRouter } from "next/navigation";
import { useEffect, useState, type ReactNode } from "react";
import { readToken } from "@/stores/admin-token";
type ShellAuthGateProps = {
children: ReactNode;
};
/**
* 壳内路由:仅客户端可读 `localStorage` Token无 Token 时跳转登录。
* 注意:首屏仍可能先出现服务端渲染的页面片段,再完成跳转(未用 Cookie + middleware 前无法完全避免)。
*/
export function ShellAuthGate({ children }: ShellAuthGateProps) {
const router = useRouter();
const [allowed, setAllowed] = useState(false);
useEffect(() => {
const token = readToken();
if (!token) {
router.replace("/admin/login");
return;
}
queueMicrotask(() => {
setAllowed(true);
});
}, [router]);
if (!allowed) {
return (
<div className="flex min-h-[50vh] w-full flex-1 items-center justify-center text-sm text-muted-foreground">
</div>
);
}
return children;
}