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/solid"
import { createQuery } from "@tanstack/solid-query"

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

Server-side prefetching

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

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

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

For server loaders, import from @better-auth-ui/solid/server. These helpers call your Better Auth server instance directly and require the request params passed to auth.api.getSession, typically request headers.

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

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

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

Today the Solid server entrypoint provides true server-auth helpers for session. Other query families use client/router helpers from @better-auth-ui/solid unless explicitly exported from @better-auth-ui/solid/server.

Invalidation

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

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

Client params

Prop

Type

Server params

Prop

Type

Last updated on

On this page