新增路由管理
Change-Id: I8139fd09f135c42944f60ca473ee208e69549dc2
diff --git a/Merge/front/src/router/Guards.jsx b/Merge/front/src/router/Guards.jsx
new file mode 100644
index 0000000..3fa7408
--- /dev/null
+++ b/Merge/front/src/router/Guards.jsx
@@ -0,0 +1,24 @@
+// src/router/Guards.jsx
+import React from 'react'
+import { Navigate, Outlet, useLocation } from 'react-router-dom'
+import { getAuthToken, getUserInfo } from '../utils/auth'
+
+/** 登录检查:有 token 才放行,否则跳到 /login */
+export function RequireAuth({ children }) {
+ const token = getAuthToken()
+ const loc = useLocation()
+ if (!token) {
+ return <Navigate to="/login" state={{ from: loc }} replace />
+ }
+ return children ?? <Outlet />
+}
+
+/** 角色检查:只有 allowedRoles 内的角色才能访问 */
+export function RequireRole({ allowedRoles, children }) {
+ const user = getUserInfo()
+ // user 里应该有 .role
+ if (!user || !allowedRoles.includes(user.role)) {
+ return <Navigate to="/login" replace />
+ }
+ return children ?? <Outlet />
+}