Better Auth uses traditional cookie-based session management. A session token is stored in a cookie and sent to the server on every request. The server verifies the session and returns user data if the session is valid.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/better-auth/better-auth/llms.txt
Use this file to discover all available pages before exploring further.
Session table fields
Thesession table stores the following information:
id— Unique session identifier.token— The session token, also used as the session cookie value.userId— The associated user’s ID.expiresAt— Session expiry date.ipAddress— Client IP address (from the request).userAgent— Client user agent header.
Session expiry
Sessions expire after 7 days by default. When a session is used and theupdateAge threshold is reached, the expiry is extended by expiresIn.
auth.ts
Disable session refresh
Prevent the session expiry from being extended on use:auth.ts
Defer session refresh
By default,GET /get-session performs a database write to refresh the session, which can cause issues with read-replica setups. When deferred, GET becomes read-only and returns needsRefresh: true when a refresh is needed. The client automatically calls POST to perform the refresh.
auth.ts
Session freshness
Some endpoints require a fresh session — one whosecreatedAt is within the freshAge limit. The default freshAge is 1 day.
auth.ts
auth.ts
Managing sessions
Get session
Use session (reactive)
useSession provides a reactive way to access the current session in framework-specific clients:
List sessions
Returns all active sessions for the current user:Revoke a session
Revoke a specific session by its token:Revoke other sessions
Revoke all sessions except the current one:Revoke all sessions
Update session
If you have additional fields configured on the session, update them withupdateSession. Core fields (token, userId, expiresAt, etc.) cannot be changed through this endpoint.
auth-client.ts
server.ts
Revoke sessions on password change
Cookie cache
Calling the database on everyuseSession or getSession call is expensive. Cookie caching stores session data in a short-lived, signed cookie — similar to a JWT access token paired with a refresh token. The server validates the cookie locally instead of querying the database, and a short maxAge ensures session data is refreshed regularly.
auth.ts
Cookie cache encoding strategies
| Strategy | Size | Readable | Interoperable | Use case |
|---|---|---|---|---|
compact (default) | Smallest | Yes | No | Performance-critical, internal use |
jwt | Medium | Yes | Yes | JWT compatibility, external integrations |
jwe | Largest | No | Yes | Sensitive data, maximum security |
auth.ts
Sessions in secondary storage
When you configure a secondary storage, Better Auth stores sessions there instead of the primary database.Force database storage
To store sessions in the primary database even when secondary storage is configured:auth.ts
Preserve revoked sessions
By default, revoking a session removes it from secondary storage. SetpreserveSessionInDatabase to keep a record of revoked sessions in the database:
auth.ts
Stateless session management
Better Auth supports fully stateless sessions — session data is stored in a signed or encrypted cookie, and the server never queries a database to validate it.Basic stateless setup
If you omit thedatabase option, Better Auth automatically enables stateless mode:
auth.ts
auth.ts
Automatic refresh with refreshCache
The refreshCache option controls automatic cookie renewal without a database query:
false(default) — No automatic refresh; expires whenmaxAgeis reached.true— Refreshes automatically when 80% ofmaxAgehas elapsed.object— Custom configuration with anupdateAgeproperty.
auth.ts
Versioning stateless sessions
Stateless sessions cannot be individually invalidated. To invalidate all sessions at once, increment theversion value and redeploy:
auth.ts
Stateless with secondary storage
Combine stateless cookie validation with secondary storage for session revocation support:auth.ts
Customizing the session response
Use thecustomSession plugin to extend the data returned by getSession and useSession:
auth.ts
customSessionClient plugin:
auth-client.ts
Custom session fields are not included in cookie or secondary storage caches. Your custom session function is called every time a session is fetched.