Protected/Private Routes
In this page:
function RequireAuth({ children }) {
if (!isLoggedIn) return <Navigate to="/login" replace />;
return children;
}
<Route path="/private" element={<RequireAuth><PrivatePage /></RequireAuth>} />
Building a RequireAuth Wrapper
A RequireAuth component checks whether the user is authenticated, and either renders its children (the protected page) or redirects elsewhere.
Wrapping a Route's element in this component blocks direct URL access to protected pages, not just hiding a link to them.
Example: Building a RequireAuth Wrapper
// Run in your local React project (npm install required)
import { Navigate } from 'react-router-dom';
function RequireAuth({ isLoggedIn, children }) {
if (!isLoggedIn) return <Navigate to="/login" replace />;
return children;
}
⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.
Using Navigate for Declarative Redirects
The Navigate component performs a redirect as part of what it renders, rather than calling an imperative function like useNavigate(). Rendering <Navigate to='/login' /> immediately redirects the browser to /login — useful inside conditional JSX like the RequireAuth wrapper.
Example: Using Navigate for Declarative Redirects
// Run in your local React project (npm install required)
import { Routes, Route, Navigate } from 'react-router-dom';
function Dashboard({ isLoggedIn }) {
if (!isLoggedIn) return <Navigate to="/login" />;
return <p>Welcome to your dashboard</p>;
}
⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.
Wrapping a Protected Route in the Route Tree
Combining RequireAuth with your route definitions means wrapping a protected page's element in <RequireAuth>...</RequireAuth> right in the Route declaration.
This keeps the auth check colocated with the routing configuration, easy to see at a glance which routes are protected.
Example: Wrapping a Protected Route in the Route Tree
// Run in your local React project (npm install required)
import { Routes, Route } from 'react-router-dom';
function RequireAuth({ isLoggedIn, children }) {
return isLoggedIn ? children : <p>Redirecting to login...</p>;
}
function Dashboard() { return <p>Protected dashboard content</p>; }
function AppRoutes({ isLoggedIn }) {
return (
<Routes>
<Route path="/dashboard" element={<RequireAuth isLoggedIn={isLoggedIn}><Dashboard /></RequireAuth>} />
</Routes>
);
}
⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.
- Only hiding the link to a protected page, without actually blocking direct URL access to it.
- Checking authentication status after the protected content has already briefly rendered, causing a flash of private content.
- Forgetting to preserve the originally-requested URL so the user lands back there after logging in.
- A protected route wraps its element in a check that redirects unauthenticated users elsewhere.
- This is commonly built as a small wrapper component checking a boolean (like isLoggedIn) before rendering its children.
- The Navigate component performs a redirect declaratively, as part of what a component renders.
- True security still requires server-side checks — client-side route protection only controls the UI.
Requires npm install react-router-dom — not available via CDN in this sandbox.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: