35 lines
808 B
React
35 lines
808 B
React
import React from 'react';
|
|||
|
|
import { Navigate, useLocation } from 'react-router-dom';
|
||
|
|
import { Spin } from 'antd';
|
||
|
|
import { useAuth } from '../context/AuthContext';
|
||
|
|
|
||
|
|
const ProtectedRoute = ({ children, requiredPermission }) => {
|
||
|
|
const { user, token, loading, initialized } = useAuth();
|
||
|
|
const location = useLocation();
|
||
|
|
|
||
|
|
if (!initialized) {
|
||
|
|
return (
|
||
|
|
<div style={{
|
||
|
|
display: 'flex',
|
||
|
|
justifyContent: 'center',
|
||
|
|
alignItems: 'center',
|
||
|
|
height: '100vh'
|
||
|
|
}}>
|
||
|
|
<Spin size="large" tip="加载中..." />
|
||
|
|
</div>
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!token) {
|
||
|
|
return <Navigate to="/login" state={{ from: location }} replace />;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (requiredPermission && !user) {
|
||
|
|
return <Navigate to="/" replace />;
|
||
|
|
}
|
||
|
|
|
||
|
|
return children;
|
||
|
|
};
|
||
|
|
|
||
|
|
export default ProtectedRoute;
|