import { useRoutes } from "react-router-dom";
// ...
export default function App() {
// check auth here?
const routes = useRoutes([
{
path: "/",
element: <HomePage />
},
{
path: "/login",
element: <LoginPage />
}
]);
return routes;
}
I want to limit available pages based on user roles and login status.
As the example code shows, can I add a auth check logic (maybe an AuthProvider
), then if auth status changes, create the routes based on user role?
const [routes, setRoutes] = useState([])
useEffect(
() => {
if (user.role === 'admin') {
setRoutes(...)
} else if ...
},
[routes]
)
Is this a good idea?