| import { Navigate } from 'react-router-dom'; |
| import { useEffect, useState } from 'react'; |
| import { Spin } from 'antd'; |
| |
| const ProtectedRoute = ({ children }) => { |
| const [loading, setLoading] = useState(true); |
| const [isAuthenticated, setIsAuthenticated] = useState(false); |
| |
| useEffect(() => { |
| // 简单检查是否有token |
| const token = localStorage.getItem('token'); |
| if (token) { |
| setIsAuthenticated(true); |
| } |
| setLoading(false); |
| }, []); |
| |
| if (loading) { |
| return ( |
| <div className="flex justify-center items-center h-screen"> |
| <Spin spinning={loading} fullScreen tip="加载中..."> |
| {children} |
| </Spin> |
| </div> |
| ); |
| } |
| |
| if (!isAuthenticated) { |
| return <Navigate to="/login" replace />; |
| } |
| |
| return children; |
| }; |
| |
| export default ProtectedRoute; |