API reference
The public surface of
@thunderid/express, as documented in its reference pages.ThunderID MiddlewareThe `thunderID()` function returns Express middleware that initializes a `ThunderIDExpressClient` and attaches it to both `req.thunderIDAuth` and `res.thunderIDAuth`.
const express = require('express');
const cookieParser = require('cookie-parser');
const {thunderID, handleSignIn, handleSignOut} = require('@thunderid/express');
const app = express();
app.use(cookieParser());
app.use(express.json());
app.use(
thunderID({
baseUrl: 'https://localhost:8090',
clientId: '<your-client-id>',
clientSecret: '<your-client-secret>',
}),
);
app.get('/login', handleSignIn());
app.get('/logout', handleSignOut());handleFlow()The `handleFlow()` function returns an Express route handler that advances the embedded sign-in flow and responds with JSON.
const express = require('express');
const cookieParser = require('cookie-parser');
const {thunderID, handleFlow, handleSignIn, handleSignOut} = require('@thunderid/express');
const app = express();
app.use(cookieParser());
app.use(express.json());
app.use(
thunderID({
baseUrl: 'https://localhost:8090',
clientId: '<your-client-id>',
clientSecret: '<your-client-secret>',
mode: 'embedded',
}),
);
app.post('/flow/sign-in', handleFlow());
app.get('/login', handleSignIn());
app.get('/logout', handleSignOut());handleSignIn()The `handleSignIn()` function returns an Express route handler for the sign-in path.
const express = require('express');
const cookieParser = require('cookie-parser');
const {thunderID, handleSignIn} = require('@thunderid/express');
const app = express();
app.use(cookieParser());
app.use(
thunderID({
baseUrl: 'https://localhost:8090',
clientId: '<your-client-id>',
clientSecret: '<your-client-secret>',
afterSignInUrl: 'http://localhost:3000/login',
}),
);
app.get('/login', handleSignIn());handleSignOut()The `handleSignOut()` function returns an Express route handler for the sign-out path.
const express = require('express');
const cookieParser = require('cookie-parser');
const {thunderID, handleSignOut} = require('@thunderid/express');
const app = express();
app.use(cookieParser());
app.use(
thunderID({
baseUrl: 'https://localhost:8090',
clientId: '<your-client-id>',
clientSecret: '<your-client-secret>',
afterSignOutUrl: 'http://localhost:3000/logout',
}),
);
app.get('/logout', handleSignOut());protect()The `protect()` function returns Express middleware that blocks unauthenticated requests.
const {protect} = require('@thunderid/express');
app.get('/protected', protect(), (_req, res) => {
res.send('Protected content');
});ThunderIDExpressClient`ThunderIDExpressClient` is the Express-aware client class exported by `@thunderid/express`. It extends `ThunderIDNodeClient` and adds Express-specific request and configuration access.
const {ThunderIDExpressClient} = require('@thunderid/express');ExpressClientConfig`ExpressClientConfig` is the main configuration type for `@thunderid/express`.
const {thunderID} = require('@thunderid/express');
app.use(
thunderID({
baseUrl: 'https://localhost:8090',
clientId: '<your-client-id>',
clientSecret: '<your-client-secret>',
afterSignInUrl: 'http://localhost:3000/login',
afterSignOutUrl: 'http://localhost:3000/logout',
sessionCookie: {
expiryTime: 28800,
httpOnly: true,
sameSite: 'lax',
secure: false,
},
}),
);CookieConfigThe Express SDK exports `CookieConfig` and `SESSION_COOKIE_NAME` for the session cookie behavior used by the SDK.
const {SESSION_COOKIE_NAME} = require('@thunderid/express');
app.get('/session-id', (req, res) => {
res.json({sessionId: req.cookies?.[SESSION_COOKIE_NAME] ?? null});
});