Protecting Frontend Pages
Protecting frontend pages means stopping a logged-out visitor from seeing screens meant only for signed-in users, such as a dashboard or profile page. In a React single page application you do this at the routing level: if there is no valid token, the protected route redirects to the login page instead of rendering.
What frontend protection can and cannot do
Everything in a browser can be inspected and changed by the user, so frontend route guards are about user experience, not security. They keep people from landing on a half-empty page that fails on every request. The real protection must live on the server, which checks the token on every API call and refuses to return private data without it. Always build both layers.
Building a guard component
With React Router, a clean pattern is a small wrapper component that reads the auth state and either renders its children or returns a Navigate element that redirects. It also remembers where the user wanted to go, so the login page can send them back afterwards.
import { useContext } from "react";
import { Navigate, useLocation } from "react-router-dom";
import { AuthContext } from "./AuthContext";
function RequireAuth({ children }) {
const { token } = useContext(AuthContext);
const location = useLocation();
if (!token) {
return <Navigate to="/login" replace state={{ from: location }} />;
}
return children;
}
// Route setup
<Routes>
<Route path="/login" element={<LoginPage />} />
<Route
path="/dashboard"
element={
<RequireAuth>
<Dashboard />
</RequireAuth>
}
/>
</Routes>
The replace prop matters. Without it, the protected URL stays in the browser history, so pressing Back after logging in would bounce the user to the guard again and straight back to login.
Sending users back after login
Inside the login page, read location.state?.from?.pathname and, after a successful login, call navigate(from || "/dashboard", { replace: true }). This small touch makes deep links work: someone who clicks a link to a private page, logs in, and lands exactly where they intended.
Hiding links as well as routes
Guarding the route is only half the job. Navigation menus should also react to the auth state: show "Login" and "Sign up" when there is no token, and show "Dashboard" and "Logout" when there is one. A simple conditional in the header component reading the same context keeps the interface consistent.
The flash-of-redirect problem
A very common bug appears once you restore the login from localStorage. On the first render, the token state is still null because the restore happens in an effect. The guard sees no token and redirects to login, even though the user is actually signed in. A moment later the token loads, but the user is already on the wrong page.
The fix is to track a separate "checking" flag. Start it as true, set it to false once the restore attempt is finished, and make the guard render nothing (or a spinner) while the check is in progress. Alternatively, read localStorage synchronously in the useState initialiser so the token is available on the very first render.
Checklist for a protected page
- The route is wrapped in a guard that redirects when there is no token.
- The redirect uses
replaceand stores the original location. - The guard waits until the saved session has been checked.
- Menus show or hide links based on the same auth state.
- The server independently rejects requests without a valid token.
Takeaway: a route guard is a convenience for honest users; pair it with server-side checks, avoid redirecting before the session is restored, and send people back to the page they asked for.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.