Skip to main content
Sonamu authentication is built on better-auth, and better-auth owns the session lifecycle: it issues the session cookie, stores sessions, refreshes and expires them, and revokes them. This page covers how those sessions reach your APIs and how to configure and control them through 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

  1. A client signs in through a better-auth endpoint such as POST /api/auth/sign-in/email.
  2. better-auth creates a row in the sessions table (through Sonamu’s Knex adapter) and sets a signed session cookie.
  3. For every HTTP and WebSocket request, Sonamu calls better-auth’s getSession with the request headers and passes the result to the context as user and session.
  4. Signing out or revoking a session deletes it, and the next request resolves user and session as null.
The 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.
The session object uses better-auth’s field names (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 under server.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:
With the cookie cache enabled, a revoked session can remain valid on other devices until the cached cookie expires (maxAge). Keep maxAge short.
Secondary storage keeps sessions in a key-value store such as Redis. Implement get, set, and delete; set receives the TTL in seconds.
When secondary storage is set, better-auth stores sessions only there unless 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 calling authClient.revokeOtherSessions() on the client after signIn.email resolves.

Cleaning Up Expired Sessions

Expired sessions are rejected, but their rows stay in the sessions table. Delete them periodically with a scheduled workflow.

Cautions

  1. Set better-auth’s secret (or the BETTER_AUTH_SECRET environment variable) to a random value such as openssl rand -base64 32; it signs session cookies and tokens. Outside test (development, staging, production), Sonamu refuses to start the server when the secret is missing or a publicly known value (the old example fallback or better-auth’s default). If you ran with a public value, rotate the secret and revoke existing sessions.
  2. Set baseURL and trustedOrigins to the URLs your clients actually use.
  3. Serve production traffic over HTTPS so better-auth can issue secure cookies.
  4. Keep cookieCache.maxAge short if you rely on revoking sessions.

Next Steps

Authentication Setup

Building authentication system

Protected Routes

Permission control with Guards

Context

Learn more about Context

Workflows

Task scheduling