wu | 2f28f67 | 2025-06-19 14:29:30 +0800 | [diff] [blame] | 1 | // src/components/RequireAuth.jsx |
2 | import React from 'react' | ||||
3 | import { Navigate, useLocation } from 'react-router-dom' | ||||
4 | import { isLoggedIn } from '../utils/auth' | ||||
5 | |||||
6 | export function RequireAuth({ children }) { | ||||
7 | const location = useLocation() | ||||
8 | if (!isLoggedIn()) { | ||||
9 | // 未登录跳到 /login,并保存当前尝试访问的地址 | ||||
10 | return <Navigate to="/login" replace state={{ from: location }} /> | ||||
11 | } | ||||
12 | return children | ||||
13 | } |