Load current user state
A client page needs the signed-in user's identity and session details.
Better Auth · Function
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.
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.
await authClient.getSession()A session response whose data contains user and session objects; server calls require request headers.
getSession when code needs the currently active authenticated session.customSession plugin can change the shape or contents of the session response.useSession action instead of repeatedly fetching manually.user and session properties rather than expecting a single user value.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.
A client page needs the signed-in user's identity and session details.
A server route must determine which user made a request.
A customized session payload is required across the application.
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.
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.
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.
useSessionauthClientauth.api.getSessioncustomSessionfromNodeHeaders