44 lines
1.4 KiB
TypeScript
44 lines
1.4 KiB
TypeScript
import { Navigate, Route, Routes } from "react-router-dom";
|
|
import Login from "../pages/Login";
|
|
import ResetPassword from "../pages/ResetPassword";
|
|
import AppLayout from "../layouts/AppLayout";
|
|
import { menuRoutes, extraRoutes } from "./routes";
|
|
import { useAuth } from "../hooks/useAuth";
|
|
|
|
function RequireAuth({ children }: { children: JSX.Element }) {
|
|
const { isAuthed, profile } = useAuth();
|
|
if (!isAuthed) {
|
|
return <Navigate to="/login" replace />;
|
|
}
|
|
// 强制改密拦截
|
|
if (profile?.pwdResetRequired === 1) {
|
|
return <Navigate to="/reset-password" replace />;
|
|
}
|
|
return children;
|
|
}
|
|
|
|
export default function AppRoutes() {
|
|
return (
|
|
<Routes>
|
|
<Route path="/login" element={<Login />} />
|
|
<Route path="/reset-password" element={<ResetPassword />} />
|
|
<Route
|
|
path="/"
|
|
element={
|
|
<RequireAuth>
|
|
<AppLayout />
|
|
</RequireAuth>
|
|
}
|
|
>
|
|
{menuRoutes.map((route) => (
|
|
<Route key={route.path} index={route.path === "/"} path={route.path === "/" ? undefined : route.path.slice(1)} element={route.element} />
|
|
))}
|
|
{extraRoutes && extraRoutes.map((route) => (
|
|
<Route key={route.path} path={route.path.startsWith('/') ? route.path.slice(1) : route.path} element={route.element} />
|
|
))}
|
|
</Route>
|
|
<Route path="*" element={<Navigate to="/" replace />} />
|
|
</Routes>
|
|
);
|
|
}
|