Keeping sessions in Redis
~7 minBy default the SDK encrypts the whole session, tokens included, into the session cookie. With a `storage`, the access, refresh, and ID tokens are kept in Redis, or any other key-value store, and the cookie carries only a sealed reference to the session. A token reaches the browser only if the application sends it there itself.
Compared with the session cookie
Keep sessions in Redis
Install a Redis client
The SDK does not depend on a Redis client, so install the one the application uses. Bothioredis and redis (node-redis) work.npm install ioredis
Create the storage
createRedisStorage() turns a Redis client into a storage. Create it once, in a module the provider and the proxy both import. Pass { prefix: 'my-app:' } as the second argument when several applications share one Redis database.
import Redis from 'ioredis'import { createRedisStorage } from '@thunderid/nextjs/server'export const storage = createRedisStorage(new Redis(process.env.REDIS_URL!))
Pass the storage to the provider
The provider keeps the storage on the server. It is not passed to client components.
import { ThunderIDProvider } from '@thunderid/nextjs/server'import { storage } from '@/lib/storage'export default function RootLayout({ children }: { children: React.ReactNode }) {return (<html lang="en"><body><ThunderIDProvider storage={storage}>{children}</ThunderIDProvider></body></html>)}
Pass the storage to the proxy
Next.js bundles the server components and the proxy separately, so the proxy needs the storage too. Add it as the second argument of the proxy you already have, keeping its handler. When a request reaches code that was not given the storage, the SDK logs an error asking for the same storage on both, and the request looks signed out.
import { createRouteMatcher, thunderIDProxy } from '@thunderid/nextjs/server'import { storage } from '@/lib/storage'const isProtectedRoute = createRouteMatcher(['/dashboard(.*)'])export default thunderIDProxy(async (thunderid, request) => {if (isProtectedRoute(request)) {return thunderid.protectRoute()}},{ storage },)export const config = {matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'],}
A TCP Redis client such as ioredis does not run on the Edge runtime. On Next.js 16 the proxy runs on the Node.js runtime. On Next.js 15.5 or later, set runtime to nodejs in the config of middleware.ts. On earlier releases, use a Redis client that talks HTTP.
Check it works
Sign in
In the browser's developer tools, open the session cookie. It is a single cookie of a few hundred bytes, with no .0, .1 chunks.
List the sessions in Redis
One key appears per signed-in session, named thunderid:0:<client id>:session:<session id>. It holds the session's tokens, and under claims, what the session cookie used to carry. With a prefix, put it in front of the pattern.
redis-cli -u "$REDIS_URL" --scan --pattern 'thunderid:*:*:session:*'redis-cli -u "$REDIS_URL" TTL <key>
End the session from the server
Delete the key. The next request is signed out, in every browser tab and on every instance.
redis-cli -u "$REDIS_URL" DEL <key>
How sessions behave with storage
Split the storage or use another store
Give each kind of data its own storage
storage also takes a storage for each kind of data: session (sessions and their tokens), sessionState (a sign-in in progress), transient, and persisted. A kind left out goes to session. Without session, sessions stay in the cookie and the kinds left out stay in the memory of the process. Every instance needs the same session and sessionState storage, since a sign-in can finish on a different instance from the one where it started.
export const storage = {session: createRedisStorage(sessionsRedis),sessionState: createRedisStorage(shortLivedRedis),}
Use a store other than Redis
Any object that implements Storage works. setData receives the time to live in seconds, and getData returns '{}' for a missing key and throws only when the read fails. A client that parses JSON values on read, such as @upstash/redis by default, needs that turned off (automaticDeserialization).
import type { Storage } from '@thunderid/nextjs/server'export const storage: Storage = {getData: async (key) => (await myStore.get(key)) ?? '{}',setData: (key, value, ttlSeconds) => myStore.set(key, value, { ttl: ttlSeconds }),removeData: (key) => myStore.delete(key),}