新增受保护路由组件以增强认证逻辑,更新知识库管理页面的测试用例以集成用户事件,提升测试的准确性和稳定性。同时,优化相关依赖项配置,确保与最新库版本兼容,提升代码可维护性和用户体验。
This commit is contained in:
36
client/admin/components_protected_route.tsx
Normal file
36
client/admin/components_protected_route.tsx
Normal file
@@ -0,0 +1,36 @@
|
||||
import React, { useEffect } from 'react';
|
||||
import {
|
||||
useNavigate,
|
||||
} from 'react-router';
|
||||
|
||||
|
||||
import { useAuth } from './hooks_sys.tsx';
|
||||
|
||||
|
||||
export const ProtectedRoute = ({ children }: { children: React.ReactNode }) => {
|
||||
const { isAuthenticated, isLoading } = useAuth();
|
||||
const navigate = useNavigate();
|
||||
|
||||
useEffect(() => {
|
||||
// 只有在加载完成且未认证时才重定向
|
||||
if (!isLoading && !isAuthenticated) {
|
||||
navigate('/admin/login', { replace: true });
|
||||
}
|
||||
}, [isAuthenticated, isLoading, navigate]);
|
||||
|
||||
// 显示加载状态,直到认证检查完成
|
||||
if (isLoading) {
|
||||
return (
|
||||
<div className="flex justify-center items-center h-screen">
|
||||
<div className="loader ease-linear rounded-full border-4 border-t-4 border-gray-200 h-12 w-12"></div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// 如果未认证且不再加载中,不显示任何内容(等待重定向)
|
||||
if (!isAuthenticated) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return children;
|
||||
};
|
||||
Reference in New Issue
Block a user