BETTER-AUTH. UI
Queries

useSession

Read, prefetch, and invalidate the current authenticated session.

Usage

import { useSession } from "@better-auth-ui/solid"

const session = useSession(authClient)
const user = () => session.data?.user

Pass Better Auth params and Solid Query options through the same object.

const session = useSession(authClient, () => ({
  query: { disableCookieCache: true },
  staleTime: 30_000
}))

Options factory

import { sessionOptions } from "@better-auth-ui/core"
import { createQuery } from "@tanstack/solid-query"

const session = createQuery(() => sessionOptions(authClient))

Server-side prefetching

For client and router loaders that already have an authClient, use the helpers from @better-auth-ui/core.

import { ensureSession, fetchSession, prefetchSession } from "@better-auth-ui/core"

await ensureSession(queryClient, authClient)
await prefetchSession(queryClient, authClient)
const session = await fetchSession(queryClient, authClient)

For server loaders, use @better-auth-ui/core/server. These helpers call your Better Auth server instance directly. Pass the request headers required by auth.api.getSession.

import { ensureSessionServer } from "@better-auth-ui/core/server"

import { auth } from "~/lib/auth"

const session = await ensureSessionServer(queryClient, auth, {
  headers: request.headers
})

@better-auth-ui/core/server is the canonical session server-auth entrypoint. Plugin non-session server helpers live under their plugin /server entrypoints, such as @better-auth-ui/core/plugins/organization/server.

Invalidation

import { sessionOptions } from "@better-auth-ui/core"

queryClient.invalidateQueries({
  queryKey: sessionOptions(authClient).queryKey
})

The shared key factory provides the same query key:

import { authQueryKeys } from "@better-auth-ui/core"

queryClient.invalidateQueries({ queryKey: authQueryKeys.session })

Client params

Prop

Type

Server params

Prop

Type

Last updated on

On this page