wu | 2f28f67 | 2025-06-19 14:29:30 +0800 | [diff] [blame] | 1 | // src/components/RequireRole.jsx |
2 | import React from 'react' | ||||
3 | import { Navigate } from 'react-router-dom' | ||||
4 | import { getUserInfo, isLoggedIn } from '../utils/auth' | ||||
5 | |||||
6 | export function RequireRole({ role, children }) { | ||||
7 | if (!isLoggedIn()) { | ||||
8 | // 未登录 | ||||
9 | return <Navigate to="/login" replace /> | ||||
10 | } | ||||
11 | const user = getUserInfo() | ||||
12 | if (user.role !== role) { | ||||
13 | // 角色不匹配,回首页 | ||||
14 | return <Navigate to="/" replace /> | ||||
15 | } | ||||
16 | return children | ||||
17 | } |