Skip to main content
0 reactions

How to Show the Signed-In User with Better Auth useSession

A dark indigo card titled Better Auth useSession, labelled Better Auth How-To, with the line Show who is signed in, in any React component, beside a drawing of a header showing Signed in as Lena Park and the chips authClient.useSession() and data, isPending, error.
How to show the signed-in user with Better Auth useSession.

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

SyntaxTypeScript
const { data, isPending, error, refetch } = authClient.useSession();
  • data: the session, with the person in data.user (such as data.user.name and data.user.email), or null when nobody is signed in.
  • isPending: true while 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

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:

components/user-menu.tsxTypeScript
"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 isPending is true, 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:

app/layout.tsx (excerpt)TypeScript
        <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:

The right end of the Bookshop header with two links, Sign in and Sign up.
No session: the links.

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

The right end of the Bookshop header showing Signed in as Lena Park and a Sign out button.
A session: the name from session.user.

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

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