feat: 添加三个 service 文件和 RoleGuard 组件
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* 角色路由守卫 — 拦截非授权角色访问受保护路由
|
||||
* 用法: <Route path="/users" element={<RoleGuard roles={['zhipianren']}><UserManage /></RoleGuard>} />
|
||||
*/
|
||||
import { Navigate } from 'react-router-dom'
|
||||
import useAuthStore from '../../stores/authStore'
|
||||
|
||||
function RoleGuard({ children, roles }) {
|
||||
const { user } = useAuthStore()
|
||||
|
||||
if (!user) return null // AuthGuard 已处理未登录跳转
|
||||
|
||||
if (!roles.includes(user.role)) {
|
||||
return <Navigate to="/dashboard" replace />
|
||||
}
|
||||
|
||||
return children
|
||||
}
|
||||
|
||||
export default RoleGuard
|
||||
Reference in New Issue
Block a user