To keep a Next.js page private with Better Auth, read the session on the server, inside the page itself, and send anyone without one to the sign-in page. Hiding a link in the browser isn’t enough: the check has to run on the server.
The short answer: in a server component, call auth.api.getSession({ headers: await headers() }) inside <Suspense>, and redirect("/sign-in") when it returns null.
Syntax
const session = await auth.api.getSession({
headers: await headers(),
});
if (!session) {
redirect("<sign-in path>");
}
authis your Better Auth instance, the one exported fromlib/auth.ts.auth.api.getSessionreads the session on the server. It returns the session and its user, ornullwhen nobody is signed in.headers()comes fromnext/headersand gives the headers of the current request, which carry the session cookie.redirectcomes fromnext/navigation.<sign-in path>is the page to send signed-out visitors to.
Better Auth’s Next.js integration (opens in a new tab) page documents this pattern, and Next.js documents headers (opens in a new tab) and redirect (opens in a new tab).
Step 1: Read the Session in the Page
The example is a small Next.js app with Better Auth and email and password sign-in, and one person, Alex Rivera (alex@example.com). It has a /dashboard page that greets the signed-in person. Create app/dashboard/page.tsx:
import { headers } from "next/headers";
import { redirect } from "next/navigation";
import { Suspense } from "react";
import { auth } from "@/lib/auth";
export default function DashboardPage() {
return (
<main className="mx-auto w-full max-w-xl flex-1 px-6 py-12">
<h1 className="text-2xl font-semibold">Dashboard</h1>
<Suspense fallback={<p className="mt-4">Loading…</p>}>
<Greeting />
</Suspense>
</main>
);
}
async function Greeting() {
const session = await auth.api.getSession({
headers: await headers(),
});
if (!session) {
redirect("/sign-in");
}
return <p className="mt-4">Hello, {session.user.name}!</p>;
}
DashboardPageshows the heading straight away.Greetingreads the session. If there isn’t one,redirectstops it and sends the visitor to/sign-in.- After the check,
session.user.nameis the signed-in person’s name. <Suspense>shows “Loading…” untilGreetingis ready. With Next.js 16’s Cache Components, anything that reads the request must sit inside<Suspense>.
Step 2: Open the Page
Sign in as Alex and open http://localhost:3000/dashboard:

Now open the same address in a browser where nobody is signed in: the sign-in page opens instead. The dashboard never shows its greeting to a visitor without a session.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Hiding the page’s link instead of checking the session | Anyone who types the address still sees the page | Check the session in the page itself |
Checking only a cookie, for example in proxy.ts | A made-up or ended session gets past it | Check with auth.api.getSession in the page |
Troubleshooting
| Problem | Cause | Fix |
|---|---|---|
npm run build stops with “Next.js encountered uncached or runtime data during prerendering” | The session is read outside <Suspense> | Read it in a component inside <Suspense>, as above |
| The page always sends you to sign in | There’s no session in this browser | Sign in first, in the same browser |
FAQ
Can I use this in a layout?
Yes, but keep the session read in a component inside <Suspense>, not at the layout’s top level, so the rest of the page doesn’t wait for it.
Is a redirect in a server component a real redirect?
Inside <Suspense> the page is streamed, so Next.js sends the redirect to the browser, which then opens the sign-in page. The greeting itself is never sent.
Conclusion
Reading the session with auth.api.getSession in a server component, inside <Suspense>, and redirecting when it’s missing keeps a Next.js page private.
Additional Resources
- Next.js integration (opens in a new tab) in the Better Auth documentation
- Authentication with Cache Components (opens in a new tab) in the Next.js documentation
- redirect (opens in a new tab) in Next.js
Related
Better Auth, Next.js and other names mentioned are trademarks of their respective owners. JD Bots isn’t affiliated with or endorsed by Better Auth, Inc. or Vercel.
0 comments