Keeping sessions in Redis
~2 minThe Express session cookie carries only a session id. The tokens behind it live in the client's storage, which by default is the memory of the Node.js process, so a restart signs everyone out and each instance sees only its own sessions. Give the SDK a `storage` to keep them in Redis, or any other key-value store, instead.
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
Pass the storage to the middleware
createRedisStorage() turns a Redis client into a storage. Pass { prefix: 'my-app:' } as its second argument when several applications share one Redis database.
import Redis from 'ioredis'import { createRedisStorage, thunderID } from '@thunderid/express'app.use(thunderID({baseUrl: process.env.THUNDERID_BASE_URL,clientId: process.env.THUNDERID_CLIENT_ID,clientSecret: process.env.THUNDERID_CLIENT_SECRET,storage: createRedisStorage(new Redis(process.env.REDIS_URL)),}),)
Check it works
Sign in, restart the server, and reload. You stay signed in. Each session is kept under <vendor>:0:<client id>:session:<session id>, where <vendor> is the configured vendor, thunderid by default.
redis-cli -u "$REDIS_URL" --scan --pattern 'thunderid:*:*:session:*'