server.auth.
Sonamu has no separate session plugin. Configure everything on this page in
server.auth, which
accepts better-auth options. See Authentication Setup
first if authentication is not enabled yet.How Sessions Work
- A client signs in through a better-auth endpoint such as
POST /api/auth/sign-in/email. - better-auth creates a row in the
sessionstable (through Sonamuβs Knex adapter) and sets a signed session cookie. - For every HTTP and WebSocket request, Sonamu calls better-authβs
getSessionwith the request headers and passes the result to the context asuserandsession. - Signing out or revoking a session deletes it, and the next request resolves
userandsessionasnull.
sessions table is one of the entities created by pnpm sonamu auth generate:
Reading the Session in APIs
user and session are part of the default context. Both are null when the request is
unauthenticated or when server.auth is not configured.
expiresAt, userId, ipAddress), not the
snake_case column names.
To require a login declaratively, use guards and implement the check in guardHandler. See
Protected Routes.
Configuring Session Lifetime
Set session options underserver.auth.session. All values are in seconds.
Fixed expiration: set
disableSessionRefresh: true so the session always ends expiresIn after
sign-in.
Sliding expiration (default): every use after updateAge extends the session by expiresIn.
Set updateAge: 0 to extend on every request.
Remember Me
signIn.email accepts rememberMe (default true). With rememberMe: false, better-auth issues a
session cookie that is not persisted, so the user is signed out when the browser closes.
Reducing Session Lookups
Sonamu resolves the session on every request. Two better-auth options reduce database load. Cookie cache stores a signed copy of the session in a short-lived cookie:get, set, and
delete; set receives the TTL in seconds.
storeSessionInDatabase is true, and always reads from it.
Listing and Revoking Sessions
From the Client
From an API
Sonamu.auth is the better-auth instance. Its server API takes the request headers; convert
Fastify headers with convertFastifyHeadersToStandard.
Sonamu.auth throws if server.auth is not configured.Limiting Concurrent Logins
To keep a single active session per user, revoke the other sessions right after a successful sign-in, for example by callingauthClient.revokeOtherSessions() on the client after
signIn.email resolves.
Cleaning Up Expired Sessions
Expired sessions are rejected, but their rows stay in thesessions table. Delete them periodically
with a scheduled workflow.
Cautions
Next Steps
Authentication Setup
Building authentication system
Protected Routes
Permission control with Guards
Context
Learn more about Context
Workflows
Task scheduling