blob: e217e4faf62b6a83b3b5c58c9bd0f89ea6b853da [file] [log] [blame]
wu2f28f672025-06-19 14:29:30 +08001// src/components/RequireAuth.jsx
2import React from 'react'
3import { Navigate, useLocation } from 'react-router-dom'
4import { isLoggedIn } from '../utils/auth'
5
6export 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}