Skip to main content
NextYou're reading unreleased docs. Features and APIs may change before release.View latestv1.1.x
Back to Node.js

Storage

Storage is the key-value store the SDK keeps its data in. The storage option of ThunderIDNodeConfig takes one, or a storage for each kind of data. The Next.js and Nuxt SDKs then put only a sealed reference to the session in the session cookie. The Express SDK, whose cookie already carries only a session id, keeps its sessions in the storage instead of in the memory of the process, so every instance shares them.

Import​

ts
import {createRedisStorage, CategorizedStorage, ServerSessionCookie, toLogoutRecordStore} from '@thunderid/node'
import type {RedisLikeClient, Storage, StorageConfig} from '@thunderid/node'

Interface​

ts
interface Storage {
getData(key: string): Promise<string>
setData(key: string, value: string, ttlSeconds?: number): Promise<void>
removeData(key: string): Promise<void>
}
MethodDescription
getData(key)Returns the string stored under key, or '{}' when there is none
setData(key, value, ttlSeconds?)Stores value under key. A storage that can expire values drops it after ttlSeconds. Without ttlSeconds, the value is kept until it is removed
removeData(key)Removes key. Removing a missing key is not an error

getData must return exactly the string setData stored. It throws only when the read itself fails, never for a missing key: a failed read is not taken for a missing session or logout record, so it can never let a revoked session through.

The storage Option​

ts
storage?: Storage | StorageConfig

interface StorageConfig {
session?: Storage
sessionState?: Storage
transient?: Storage
persisted?: Storage
}

One Storage holds everything. A StorageConfig gives a storage for each kind of data, and a kind left out goes to session. Without session, the kinds left out stay in the memory of the process, and the Next.js and Nuxt SDKs keep sessions in the cookie. Without storage, data stays in the memory of the process.

KindHoldsKept for
sessionSessions and their tokens, the back-channel logout index and recordssessionCookie.expiryTime (default 86400 seconds)
sessionStateA sign-in in progress: the PKCE verifier, state, and the authId of an embedded flow15 minutes, and cleared when the code is exchanged
transientShort-lived data tied to a sessionsessionCookie.expiryTime
persistedData kept across sign-outs, such as the application idUntil removed

The client configuration, the provider metadata, and the client-wide state that goes with them always stay in the memory of the process, so the client secret is never written to a shared storage. An explicit storage argument to initialize() takes precedence over the option.

Keys​

Keys are <vendor>:<instance>:<client id>:<kind>[:<id>]. An application without a client id is told apart by its application id, and one with neither gets _, so every key has the same segments. The client id and the id are URI-encoded, so a : in one cannot shift the segments. Segments are separated by :, the namespace separator of Redis, and named in kebab case, like the session cookies.

KeyHolds
thunderid:0:<client id>:session:<session id>A session: the client's tokens, and for Next.js and Nuxt, the claims behind the reference cookie under claims. One record per session
thunderid:0:<client id>:session-state:<session id>A sign-in in progress
thunderid:0:<client id>:transient:<session id>Short-lived data for a session
thunderid:0:<client id>:persistedData kept across sign-outs
thunderid:0:<client id>:logout-index:sid:<sid>Sessions by the sid of their ID token, for back-channel logout (:sub:<sub> by subject)
thunderid:logout:sid:<sid>Sessions back-channel logout ended, for the SDKs whose session lives in a cookie (:sub:<sub> by subject)

thunderid is the configured vendor.

createRedisStorage()​

ts
createRedisStorage(client: RedisLikeClient, options?: {prefix?: string}): Storage

Wraps a connected Redis client. The SDK has no Redis dependency, so the application passes its own client. Any client with get, set, del, and either setEx (node-redis) or setex (ioredis) works. A value with a time to live is written with SETEX. The Next.js SDK re-exports it from @thunderid/nextjs/server.

ParameterTypeDescription
clientRedisLikeClientA connected ioredis or redis client
options.prefixstringPrepended to every key, to tell applications apart in a shared database, such as two servers' applications with the same client id. Default: none

Throws TypeError when the client has neither setEx nor setex.

storage.ts
ts
import Redis from 'ioredis'
import {createRedisStorage} from '@thunderid/node'

export const storage = createRedisStorage(new Redis(process.env.REDIS_URL))
storage.ts
ts
import {createClient} from 'redis'
import {createRedisStorage} from '@thunderid/node'

const redis = createClient({url: process.env.REDIS_URL})
await redis.connect()

export const storage = createRedisStorage(redis)

MemoryCacheStore​

The default Storage, in the memory of the process, which honors ttlSeconds. Sessions are lost on restart and are not shared between instances.

toLogoutRecordStore()​

Adapts a Storage to the LogoutRecordStore that back-channel logout records ended sessions in. The Next.js and Nuxt SDKs use it to record logouts in their session storage by default.

ServerSessionCookie​

Seals a session for a cookie-session SDK: into the cookie itself without a storage, or into the storage with only a reference in the cookie. In the storage, the session is the client's own session record: the tokens go in its token fields (access_token, refresh_token, id_token, scope), which ThunderIDNodeClient reads too, and the other claims under claims. Removing the record, as sign-out and back-channel logout do, ends the session for every copy of the cookie. The reference is encrypted with the session secret in the same format as SessionCookie, expires with the session, and is rejected once its session is no longer in the storage.

MethodDescription
seal(claims, secret, expiresAt, sessionTtlSeconds, options?)Returns the cookie value. With options.storage, writes claims (which must include sessionId) into the session record for sessionTtlSeconds. options.accessTokenExpiresAt sets the record's access token expiry when it differs from expiresAt
unseal(token, secret, options?)Returns the session claims, from the storage when the cookie is a reference. A cookie that carries its claims is still read, so turning a storage on does not end existing sessions. ignoreExpiry skips the access token expiry, for a token refresh
destroy(sessionId, options?)Removes the session from the storage, so every copy of its cookie stops working

A token refresh seals with existing: true, so it updates a session but does not bring back one that sign-out removed before the refresh read it. Storage has no conditional write, so a sign-out that lands between the refresh's read and its write can still be undone by that write. The window is one storage round trip. | key(sessionId, options?) | The storage key of a session |

Explore with AI

ThunderID LogoThunderID Logo

Product

DocsAPIsSDKs
© Copyright Linux Foundation Europe.For web site terms of use, trademark policy and other project policies please see https://linuxfoundation.eu/en/policies.