Skip to content

Commit f0e33ec

Browse files
Add new Express.js session option
1 parent b1c6490 commit f0e33ec

3 files changed

Lines changed: 31 additions & 9 deletions

File tree

lib/middleware/index.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,16 @@ export function configureSession(options) {
4141
const sessionName = getSessionName(serviceName)
4242

4343
app.use(
44-
session({
45-
secret: sessionName,
46-
name: sessionName,
47-
resave: false,
48-
saveUninitialized: true,
49-
cookie: {
50-
maxAge: SESSION_COOKIE_MAX_AGE
51-
}
52-
})
44+
options.session ??
45+
session({
46+
secret: sessionName,
47+
name: sessionName,
48+
resave: false,
49+
saveUninitialized: true,
50+
cookie: {
51+
maxAge: SESSION_COOKIE_MAX_AGE
52+
}
53+
})
5354
)
5455
}
5556

lib/nhsuk-prototype-kit.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ export class NHSPrototypeKit {
252252
* @property {NunjucksFilters | ((env: Environment) => NunjucksFilters)} [filters] - Additional custom Nunjucks filters
253253
* @property {object} [config] - Configuration options
254254
* @property {BuildOptions} [buildOptions] - Build options
255+
* @property {RequestHandler} [session] - Middleware to use custom session store
255256
* @property {{ [key: string]: unknown }} [sessionDataDefaults] - Default data to set in the session
256257
*/
257258

lib/nhsuk-prototype-kit.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import { describe, it, before, mock } from 'node:test'
44
import { fileURLToPath } from 'node:url'
55

66
import express from 'express'
7+
import session from 'express-session'
78
import nunjucks from 'nunjucks'
9+
import request from 'supertest'
810

911
import { NHSPrototypeKit } from './nhsuk-prototype-kit.js'
1012

@@ -96,6 +98,24 @@ describe('NHSPrototypeKit', () => {
9698
assert.deepEqual(prototype.app, customApp)
9799
})
98100

101+
it('should initialize with custom express session', async () => {
102+
const genid = mock.fn()
103+
104+
const customSession = session({
105+
saveUninitialized: true,
106+
secret: 'Shhh',
107+
genid
108+
})
109+
110+
const prototype = await NHSPrototypeKit.init({
111+
session: customSession
112+
})
113+
114+
await request(prototype.app).get('/')
115+
116+
assert.equal(genid.mock.callCount(), 1)
117+
})
118+
99119
it('should set express settings', async () => {
100120
const prototype = await NHSPrototypeKit.init()
101121

0 commit comments

Comments
 (0)