Skip to main content
The AbstractSessionStore is an abstract base class for implementing custom session stores. It provides the foundation for both stateless (cookie-based) and stateful (external store) session management.

Overview

The SDK provides two built-in implementations:
  • StatelessSessionStore: Stores the entire session in an encrypted cookie (default)
  • StatefulSessionStore: Stores a session ID in a cookie and the session data in an external store
You can extend AbstractSessionStore to create custom session storage implementations.

Constructor

Options

string
required
A 32-byte, hex-encoded secret used for encrypting cookies.
boolean
default:true
A boolean indicating whether rolling sessions should be used or not.When enabled, the session will continue to be extended as long as it is used within the inactivity duration. Once the upper bound, set via the absoluteDuration, has been reached, the session will no longer be extended.
number
default:259200
The absolute duration after which the session will expire (in seconds).Once the absolute duration has been reached, the session will no longer be extended.Default: 3 days (259200 seconds).
number
default:86400
The duration of inactivity after which the session will expire (in seconds).The session will be extended as long as it was active before the inactivity duration has been reached.Default: 1 day (86400 seconds).
SessionDataStore
A custom session store implementation used to persist sessions to a data store.
The options for the session cookie.
The name of the session cookie.
The sameSite attribute of the session cookie.
The secure attribute of the session cookie.Default: depends on the protocol of the application’s base URL. If the protocol is https, then true, otherwise false.
The path attribute of the session cookie.
Specifies the value for the Domain Set-Cookie attribute. By default, no domain is set, and most clients will consider the cookie to apply to only the current domain.
The transient attribute of the session cookie. When true, the cookie will not persist beyond the current session.

Abstract Methods

These methods must be implemented by subclasses.

get

Retrieves the session data from the store.
RequestCookies | ReadonlyRequestCookies
required
The request cookies object.
Promise<SessionData | null>
Returns the session data or null if no session exists.

set

Saves the session data to the store and sets the session cookie. The iat property on the session will be used to compute the maxAge cookie value.
RequestCookies | ReadonlyRequestCookies
required
The request cookies object.
ResponseCookies
required
The response cookies object.
SessionData
required
The session data to save.
boolean
Whether this is a new session.

delete

Deletes the session from the store and clears the session cookie.
RequestCookies | ReadonlyRequestCookies
required
The request cookies object.
ResponseCookies
required
The response cookies object.

Protected Methods

These methods are available to subclasses.

epoch

Returns the time since unix epoch in seconds.
number
The current time in seconds since epoch.

calculateMaxAge

Calculates the max age of the session based on createdAt and the rolling and absolute durations.
number
required
The time the session was created (seconds since epoch).
number
The maximum age of the session in seconds.

Public Properties

string
The secret used for encrypting cookies.
The name of the session cookie.
SessionDataStore | undefined
The custom session store implementation (if provided).
The cookie configuration options.

SessionDataStore Interface

When implementing a custom session store, you must provide an object that implements the SessionDataStore interface:

get

Gets the session from the store given a session ID.
string
required
The session ID.
Promise<SessionData | null>
Returns the session data or null if no session exists.

set

Upserts a session in the store given a session ID and SessionData.
string
required
The session ID.
SessionData
required
The session data to save.

delete

Destroys the session with the given session ID.
string
required
The session ID.

deleteByLogoutToken

Deletes the session with the given logout token which may contain a session ID or a user ID, or both.
LogoutToken
required
The logout token containing sub (user ID) and/or sid (session ID).

Example: Custom Database Session Store

Example: Redis Session Store

SessionData Type

Best Practices

  1. Session Expiration: Implement proper TTL/expiration in your custom store to prevent stale sessions from accumulating.
  2. Error Handling: Implement proper error handling in your store methods. The SDK will catch errors and treat them as missing sessions.
  3. Performance: Consider implementing caching strategies for frequently accessed sessions.
  4. Security: Ensure your session store is properly secured and access is restricted.
  5. Backchannel Logout: Implement deleteByLogoutToken to support Auth0’s backchannel logout feature.
  6. Cleanup: Implement a background job to clean up expired sessions from your store.