Most pages need to know whether someone is signed in, and who. In a React or Next.js app, Better Auth’s useSession hook answers that and keeps the answer up to date. This guide shows the signed-in user with Better Auth useSession in a Next.js header.
The short answer: const { data: session, isPending } = authClient.useSession(); in a client component. While isPending is true it’s still loading; then session is either null or holds session.user.
Syntax
const { data, isPending, error, refetch } = authClient.useSession();
data: the session, with the person indata.user(such asdata.user.nameanddata.user.email), ornullwhen nobody is signed in.isPending:truewhile the session is still being fetched.error: set if fetching the session failed.refetch: fetches the session again.
The reference: Session: Client Side (opens in a new tab) in the Better Auth documentation.
Prerequisites
- Better Auth’s React client in your app:
createAuthClientfrombetter-auth/react(Client (opens in a new tab))
Step 1: Read the Session in a Component
The example is a small bookshop app whose header shows who’s signed in. Create components/user-menu.tsx:
"use client";
import Link from "next/link";
import { authClient } from "@/lib/auth-client";
export function UserMenu() {
const { data: session, isPending } = authClient.useSession();
if (isPending) {
return null;
}
if (!session) {
return (
<nav className="flex gap-4 text-sm">
<Link href="/sign-in">Sign in</Link>
<Link href="/sign-up">Sign up</Link>
</nav>
);
}
return (
<div className="flex items-center gap-4 text-sm">
<span>Signed in as {session.user.name}</span>
<button
type="button"
onClick={() => authClient.signOut()}
className="rounded border border-zinc-300 px-3 py-1 dark:border-zinc-700"
>
Sign out
</button>
</div>
);
}
"use client": hooks only work in client components.- While
isPendingistrue, the menu shows nothing, so it never flashes the wrong state. - With no session, it shows the links; with one,
session.user.name.
Step 2: Show It on Every Page
In app/layout.tsx, import the component with import { UserMenu } from "@/components/user-menu"; and add it to the header:
<header className="flex items-center justify-between border-b border-zinc-200 px-6 py-4 dark:border-zinc-800">
<Link href="/" className="font-semibold">
Bookshop
</Link>
<UserMenu />
</header>
Signed out, the header shows the links:

Sign in, and the same header shows the name, without reloading the page:

FAQ
Can I use useSession in a Server Component?
No, hooks only run in client components. On the server, use auth.api.getSession (Session: Server Side (opens in a new tab)). Part 4 of the Better Auth Tutorial uses it.
Do I need to refresh the session after signing in or out?
No. useSession updates by itself when you sign in or out with Better Auth’s client.
Conclusion
authClient.useSession() tells any client component who’s signed in, and updates when that changes. Check isPending first, then data.
Additional Resources
- Session (opens in a new tab) in the Better Auth documentation
- Client hooks (opens in a new tab)
Related
- Better Auth Tutorial — Part 3: Sign Up and Sign In with Email and Password, where this is one step of the full setup
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