feat: 添加路由保护
This commit is contained in:
38
src/components/ProtectedRouteRedirect.js
Normal file
38
src/components/ProtectedRouteRedirect.js
Normal file
@@ -0,0 +1,38 @@
|
||||
// src/components/ProtectedRouteRedirect.js - 跳转版本
|
||||
// 未登录时跳转到首页,用于三级页面(详情页)
|
||||
import React from 'react';
|
||||
import { Navigate } from 'react-router-dom';
|
||||
import { Box, VStack, Spinner, Text } from '@chakra-ui/react';
|
||||
import { useAuth } from '../contexts/AuthContext';
|
||||
|
||||
const ProtectedRouteRedirect = ({ children }) => {
|
||||
const { isAuthenticated, isLoading, user } = useAuth();
|
||||
|
||||
// 显示加载状态
|
||||
if (isLoading) {
|
||||
return (
|
||||
<Box
|
||||
height="100vh"
|
||||
display="flex"
|
||||
alignItems="center"
|
||||
justifyContent="center"
|
||||
bg="gray.50"
|
||||
>
|
||||
<VStack spacing={4}>
|
||||
<Spinner size="xl" color="blue.500" thickness="4px" />
|
||||
<Text fontSize="lg" color="gray.600">正在验证登录状态...</Text>
|
||||
</VStack>
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
|
||||
// 未登录,直接跳转到首页
|
||||
if (!isAuthenticated || !user) {
|
||||
return <Navigate to="/home" replace />;
|
||||
}
|
||||
|
||||
// 已登录,正常渲染子组件
|
||||
return children;
|
||||
};
|
||||
|
||||
export default ProtectedRouteRedirect;
|
||||
Reference in New Issue
Block a user