Skip to main content

Nuxt

Official

Server-rendered authentication for Nuxt applications, out of the box.

Keeping sessions in Redis

~3 min

By default the module encrypts the whole session, tokens included, into the session cookie. Name a Nitro storage mount in `thunderid.storage` to keep sessions there instead. The cookie then carries only a sealed reference to the session, and every instance that shares the mount shares the sessions. The `/api/auth/token` route still returns the access token to a signed-in browser.

Keep sessions in Redis

1

Point a Nitro storage mount at Redis

The mount uses unstorage's Redis driver, so the module needs no Redis client of its own. Any other unstorage driver works the same way.

nuxt.config.ts
ts
export default defineNuxtConfig({
modules: ['@thunderid/nuxt'],
nitro: {
storage: {
sessions: { driver: 'redis', url: process.env.REDIS_URL },
},
},
thunderid: {
storage: 'sessions',
},
})
2

Enable back-channel logout, if you use it

Logouts are recorded in the same mount, so they reach every instance. Name another mount in

backchannelLogout.store only to record them somewhere else.

nuxt.config.ts
ts
thunderid: {
storage: 'sessions',
backchannelLogout: { enabled: true },
}
3

Check it works

Sign in, then list the sessions. One key appears per signed-in session, named thunderid:0:<client id>:session:<session id>. Deleting it signs that session out on every instance.

bash
redis-cli -u "$REDIS_URL" --scan --pattern 'thunderid:*:*:session:*'
redis-cli -u "$REDIS_URL" DEL <key>

How sessions behave with storage

Sign-inWrites the session to the mount and sets the reference cookie. The sign-in in progress is kept there for 15 minutes, so the callback can land on any instance
Token refresh`getValidAccessToken()` writes the new tokens to the mount. When another instance refreshed first and the refresh token rotated, it uses the tokens that instance stored
Sign-outRemoves the session from the mount. When that fails, the cookies are kept and sign-out reports an error, so it can be tried again
Existing sessionsA cookie issued before `storage` was set carries its own session and is still read until it expires
Refresh during sign-outA refresh that read the session just before sign-out removed it can write it back, within one storage round trip

Split the storage by kind of data

`storage` also takes a mount for each kind of data, such as `{ session: 'sessions', sessionState: 'sign-ins' }`. 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, so include `session` to share sessions between instances.

KindHolds
sessionSessions and their tokens, and the back-channel logout records
sessionStateA sign-in in progress: the PKCE verifier, `state`, and the `authId` of an embedded flow
transientShort-lived data tied to a session
persistedData kept across sign-outs

Beyond this guide

Module configuration

Storage reference

Nitro storage

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.