Skip to main content
0 reactions

How to Sign Out a User with Better Auth

A dark indigo card titled Sign Out with Better Auth, labelled Better Auth How-To, beside a drawing of a header showing Signed in as Lena Park with a Sign out button, an arrow, and the same header after signing out with Sign in and Sign up buttons, above the chip authClient.signOut().
How to sign out a user with Better Auth.

Signing out ends the session in this browser. With Better Auth it’s one call, authClient.signOut(), and any part of the page that shows the session updates by itself. This guide adds a Better Auth sign-out button to a Next.js app and shows the result.

The short answer: call authClient.signOut() from a button in a client component. Better Auth ends the session, and useSession switches to signed out.

Syntax

SyntaxTypeScript
await authClient.signOut({
  fetchOptions: {
    onSuccess: () => {
      // for example, open the sign-in page
    },
  },
});
  • authClient.signOut() ends the current session. The options are optional.
  • fetchOptions.onSuccess: code to run once the session has ended, such as opening another page.

The reference: Signout (opens in a new tab) in the Better Auth documentation.

Prerequisites

  • a Next.js app with Better Auth’s client (createAuthClient from better-auth/react) and a way to sign in

Step 1: Add a Sign Out Button

The example is a small bookshop app. Its header shows who’s signed in, so that’s where the button goes. 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>
  );
}
  • The button’s onClick calls authClient.signOut().
  • authClient.useSession() notices the change, so the menu switches to the Sign in and Sign up links without any other code.

Put <UserMenu /> in your layout’s header, so it’s on every page.

Step 2: Sign Out

Sign in, and the header shows the name and the button:

The right end of the Bookshop header showing Signed in as Lena Park and a Sign out button.
Signed in, with the Sign out button.

Click Sign out. The header switches back to the links:

The right end of the Bookshop header with two links, Sign in and Sign up.
Signed out: the links are back.

Better Auth deletes this browser’s session from the database. Sessions on the person’s other devices stay signed in.

FAQ

How do I open another page after signing out?

Pass fetchOptions.onSuccess, as in the Syntax section, and open the page there, for example with Next.js’s router.push.

How do I sign someone out on every device?

Better Auth’s revokeSessions ends all of the person’s sessions (Revoke All Sessions (opens in a new tab)). Part 5 of the Better Auth Tutorial covers sessions.

Conclusion

authClient.signOut() ends the session in this browser, and everything that reads the session with useSession updates by itself.

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