refactor: 替换管理员登录组件并更新会话管理
This commit is contained in:
40
src/components/admin/auth-gate.tsx
Normal file
40
src/components/admin/auth-gate.tsx
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user