01. How do you prevent a login page from being accessible after the user is logged in?
📄 Notesmedium#auth#routing#react-router#guards
Short answer
Add a guest-only guard to the login route: if the user is already authenticated, redirect them (with replace) to the dashboard or the page they came from. In React Router v6.4+/v7 the cleanest place is a route loader that throws redirect(), or a small GuestRoute wrapper that renders Navigate. In Next.js, do it in middleware or the server component before rendering. Also make sure the auth state is known before deciding, to avoid a flash of the login form.
Why it happens
- Routes are just URLs; nothing stops a logged-in user from typing
/loginor pressing Back. - After login,
navigate("/dashboard")withoutreplaceleaves/loginin history, so Back returns to it. - Auth state often loads asynchronously (token refresh,
/mecall). If the guard checks before it's loaded, it sees "logged out" and shows the form briefly.
How to fix it
- Keep a single source of truth for auth (context, Redux slice, or query cache) with a
status:"loading" | "authenticated" | "guest". - Guard the route with a loader redirect or a wrapper component.
- Use
replacewhen redirecting after login so/loginisn't in history. - Redirect back to the original page using a
fromvalue or?redirectTo=, validated to be a same-site path. - Mirror it on the server for SSR apps so the HTML for
/loginis never sent to logged-in users.
// Option A: component guard (React Router v6/v7)
import { Navigate, Outlet, useLocation } from "react-router";
import { useAuth } from "./auth";
export function GuestRoute() {
const { status } = useAuth();
const location = useLocation();
if (status === "loading") return <FullPageSpinner />;
if (status === "authenticated") {
const to = location.state?.from?.pathname ?? "/dashboard";
return <Navigate to={to} replace />;
}
return <Outlet />;
}
// routes
// { element: <GuestRoute />, children: [{ path: "/login", element: <Login /> }] }
// Option B: data router loader (runs before rendering, no flash)
import { redirect } from "react-router";
export async function loginLoader() {
const user = await getCurrentUser(); // cached / reads session
if (user) throw redirect("/dashboard");
return null;
}
// Option C: Next.js middleware
import { NextResponse } from "next/server";
export function middleware(req) {
const hasSession = req.cookies.has("session");
if (hasSession && req.nextUrl.pathname === "/login") {
return NextResponse.redirect(new URL("/dashboard", req.url));
}
}
export const config = { matcher: ["/login", "/signup"] };
After a successful login:
navigate(redirectTo ?? "/dashboard", { replace: true });
How to verify
- Log in, then type
/loginin the address bar: you land on the dashboard. - Log in, then press Back: you don't see the login form.
- Hard-refresh
/loginwhile logged in: no flash of the form. - Try
?redirectTo=https://evil.com: it should be rejected (open-redirect check). - Log out:
/loginis reachable again.
Follow-up questions
- Is a client-side guard secure? No, it's UX only. The API must still authorize every request; guards just decide what to show.
- Where do you store the token? Prefer an httpOnly, Secure, SameSite cookie set by the server over
localStorage, to limit XSS token theft. - How do you protect private routes? The mirror image: a
ProtectedRoutethat redirects guests to/loginwithstate={{ from: location }}.
Quick recap
- Guest-only guard on
/loginand/signup. - Wait for auth status before deciding.
- Redirect with
replaceto keep history clean. - Prefer loader or middleware redirects to avoid flashes.
- Server-side authorization is still required.