San3yuan | 4d0e803 | 2025-04-04 17:21:40 +0800 | [diff] [blame^] | 1 | import { Navigate, Outlet } from 'react-router-dom' |
2 | import React from 'react' | ||||
3 | |||||
4 | interface PrivateRouteProps { | ||||
5 | isAllowed: boolean | ||||
6 | redirectPath?: string | ||||
7 | children?: React.ReactNode | ||||
8 | } | ||||
9 | |||||
10 | const PrivateRoute = ({ | ||||
11 | isAllowed, | ||||
12 | redirectPath = '/login', | ||||
13 | children | ||||
14 | }: PrivateRouteProps) => { | ||||
15 | if (!isAllowed) { | ||||
16 | return <Navigate to={redirectPath} replace /> | ||||
17 | } | ||||
18 | |||||
19 | return children ? children : <Outlet /> | ||||
20 | } | ||||
21 | |||||
22 | export default PrivateRoute |