Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Better Auth · Function

getSession

getSession retrieves the currently active Better Auth session. On the client it is called through authClient; on the server, use auth.api.getSession with request headers.

How it works

The client method asks Better Auth for the currently active session. It returns session data containing user and session objects; server-side access instead uses the auth API and requires request headers.

Syntax

await authClient.getSession()

Returns

A session response whose data contains user and session objects; server calls require request headers.

Key points

  • Use getSession when code needs the currently active authenticated session.
  • The session is cookie-based, so the request carries the session information to the server.
  • A valid session produces user data after server-side verification.
  • The customSession plugin can change the shape or contents of the session response.
  • For reactive client UI updates, use the separate useSession action instead of repeatedly fetching manually.

Common mistakes

  • Read the returned data through its user and session properties rather than expecting a single user value.

Example: Read the current client session

import { authClient } from "@/lib/auth-client"

const { data: session, error } = await authClient.getSession()

if (error) {
  console.error(error)
} else if (session) {
  console.log(session.user, session.session)
}

Calls the client method and reads the returned user and session objects when available.

When to use it

Load current user state

A client page needs the signed-in user's identity and session details.

Authenticate an incoming request

A server route must determine which user made a request.

Use custom session data

A customized session payload is required across the application.

Interview questions

QWhat does getSession do?

The client method retrieves the current active session. Its returned data exposes both user and session objects.

Follow-up: How would you call it on the server? Use auth.api.getSession and pass the request headers in the options object.

QWhat is the basic client invocation?

A client call is made with authClient.getSession(). The result can be destructured into data, commonly aliased as session, and error.

Follow-up: What should a server-side implementation provide? The headers containing the user's session information.

QHow does server usage differ from client usage?

On the server, use the Better Auth instance's API: auth.api.getSession({ headers: ... }). The headers are needed so Better Auth can identify the request's session.

Follow-up: How can Node request headers be adapted in Express? Use the documented fromNodeHeaders helper before passing them.

  • useSession
  • authClient
  • auth.api.getSession
  • customSession
  • fromNodeHeaders

Source: Session Management. Checked 2026-10-15.