I'm trying to create a dynamic "index" page whereby if the user is authenticated, the "/" route shows a dashboard component and if they are not authenticated, the "/" route shows a landing page; effectively having the same path for two different pages.
This is easy enough if I check whether the user is authenticated in the component itself; however, I'm trying to use nested routes.
The goal is: one set of public routes (e.g., "/", "/login", "/signup") and one set of private routes ("/", "/settings", "/profile") -- both of which are nested under the "/" route.
I'm just not sure how to achieve this using react router v6.
My Parent Auth Route is very simple:
import { routeConstants } from '@src/constants';
import { useAuth } from '@src/hooks';
import { Navigate, Outlet } from 'react-router-dom';
const AuthRouter = () => {
const { user } = useAuth();
if (!user) {
return <Navigate to={routeConstants.Routes.LogIn} replace />;
}
return <Outlet />;
};
export default AuthRouter;
App.ts
{/* Private Routes */}
<Route element={<AuthRouter />}>
<Route path={RouteConstants.Index} element={<Pages.Home />} />
</Route>
I just can't seem to render a different "/" page based on auth status using nested routes.