Course map
Stage 1: App Router Foundations
Stage 2: The Rendering Model
Stage 3: Data, Caching and Mutations
Stage 4: Full-Stack Next.js
Stage 5: Production and Specialization

Part 15 / Topic 4 of 5

The central finding: a layout's own check does not protect the page

(protected)/layout.tsx wraps every route under app/account/(protected)/, and its own AccountNav component calls requireUser():

// harness/app/account/(protected)/layout.tsx (excerpt)
export default function ProtectedLayout({ children }: { children: ReactNode }) {
  return (
    <div>
      <Suspense fallback={<p>Checking your session…</p>}>
        <AccountNav />
      </Suspense>
      {children}
    </div>
  );
}
async function AccountNav() {
  const user = await requireUser();
  return <nav>Signed in as {user.name} ({user.role}) · <LogoutButton /></nav>;
}

It would be reasonable to assume this is enough — the nav clearly redirects an unauthenticated visitor. It is not enough, and the installed authentication.md guide says exactly why: "A layout also does not control whether the rest of the route renders. Route segments... are rendered by the router, so a layout that hides or swaps them does not stop them from running." Rather than take that on faith, this harness reproduced it directly: dashboard/page.tsx's own requireUser() call was removed (keeping the layout's AccountNav exactly as it was) and the matching proxy check (section 5) was also temporarily disabled, to isolate the Server Component layer on its own:

$ curl -s http://localhost:PORT/account/dashboard   # no session cookie at all
HTTP/1.1 200 OK
...nobody-checked-you...

The dashboard's own content — content that was supposed to be protected — rendered in full for a completely unauthenticated request. The layout's nav boundary, inspected in the same raw response, still carried its own redirect intent (NEXT_REDIRECT;replace;/account/login;307;) — the nav function itself still "wanted" to redirect. But React/Next.js render each Suspense boundary independently; one boundary throwing redirect() does not cancel a sibling boundary's own successful render. The only thing that actually gates dashboard/page.tsx's content is dashboard/page.tsx's own requireUser() call (restored immediately after this check):

// harness/app/account/(protected)/dashboard/page.tsx (excerpt)
async function DashboardContent() {
  const user = await requireUser();   // <- THIS line is the real protection
  return <section>...</section>;
}

This is the installed docs' own stated rule, made concrete rather than taken as an abstract warning: "you should do the checks close to your data source or the component that'll be conditionally rendered... This guarantees that wherever getUser() is called within your application, the auth check is performed." A shared layout's nav is convenience UI, not a security boundary — every protected page has to call the DAL itself.

Done here? The practice set for Part 15 — including the solutions — is on the part page.

Search the guide