diff --git a/src/auth0-session/config.ts b/src/auth0-session/config.ts index 8bcbb1600..f2e99dbba 100644 --- a/src/auth0-session/config.ts +++ b/src/auth0-session/config.ts @@ -208,7 +208,10 @@ export interface SessionConfig { * * **IMPORTANT** You must use a suitably unique value to prevent collisions. */ - genId?: (req: Req) => string | Promise; + genId?: ( + req: Req, + session: SessionType + ) => string | Promise; /** * If you want your session duration to be rolling, resetting everytime the diff --git a/src/auth0-session/get-config.ts b/src/auth0-session/get-config.ts index 584fc33e1..16e9f19b0 100644 --- a/src/auth0-session/get-config.ts +++ b/src/auth0-session/get-config.ts @@ -31,7 +31,7 @@ const paramsSchema = Joi.object({ autoSave: Joi.boolean().optional().default(true), name: Joi.string().token().optional().default('appSession'), store: Joi.object().optional(), - genId: Joi.function().maxArity(1).when(Joi.ref('store'), { then: Joi.required() }), + genId: Joi.function().maxArity(2).when(Joi.ref('store'), { then: Joi.required() }), storeIDToken: Joi.boolean().optional().default(true), cookie: Joi.object({ domain: Joi.string().optional(), diff --git a/src/auth0-session/session/stateful-session.ts b/src/auth0-session/session/stateful-session.ts index 6c5adba92..eb56d4305 100644 --- a/src/auth0-session/session/stateful-session.ts +++ b/src/auth0-session/session/stateful-session.ts @@ -85,7 +85,7 @@ export class StatefulSession< } if (!sessionId) { - sessionId = await genId!(req); + sessionId = await genId!(req, session); debug('generated new session id %o', sessionId); } debug('set session %o', sessionId); diff --git a/src/config.ts b/src/config.ts index 3f2817e46..fbc21eeda 100644 --- a/src/config.ts +++ b/src/config.ts @@ -218,7 +218,10 @@ export interface SessionConfig { * **IMPORTANT** If you override this, you must use a suitable value from your platform to * prevent collisions. For example, for Node: `require('crypto').randomBytes(16).toString('hex')`. */ - genId?: (req: Req) => string | Promise; + genId?: ( + req: Req, + session: SessionType + ) => string | Promise; /** * If you want your session duration to be rolling, resetting everytime the diff --git a/tests/auth0-session/session/stateful-session.test.ts b/tests/auth0-session/session/stateful-session.test.ts index 16bb6e109..f556d1b49 100644 --- a/tests/auth0-session/session/stateful-session.test.ts +++ b/tests/auth0-session/session/stateful-session.test.ts @@ -148,6 +148,17 @@ describe('StatefulSession', () => { expect(cookieJar.getCookieStringSync(baseURL)).toMatch(/^appSession=foobar\..+/); }); + it('should provide current user session to custom session id generator', async () => { + const genId = jest.fn().mockImplementation((_req, session) => session.state); + const baseURL = await setup({ ...config, session: { ...config.session, genId } }); + const cookieJar = await login(baseURL); + const genIdParams = genId.mock.calls.at(0); + expect(genIdParams.length).toEqual(2); + expect('id_token' in genIdParams.at(1)).toBeTruthy(); + const regex = `^appSession=${genIdParams.at(1).state}\..+`; + expect(cookieJar.getCookieStringSync(baseURL)).toMatch(new RegExp(regex)); + }); + it('should regenerate the session when a new user is logging in over an existing user', async () => { await store.set('foo', await getPayload()); const baseURL = await setup(config); diff --git a/tests/stateful-session.test.ts b/tests/stateful-session.test.ts index dd5eafc70..fa8041011 100644 --- a/tests/stateful-session.test.ts +++ b/tests/stateful-session.test.ts @@ -98,4 +98,15 @@ describe('next stateful session', () => { expect(Object.keys(store)).toHaveLength(1); expect(cookieJar.getCookieStringSync(baseUrl)).toMatch(/^appSession=foo\..+/); }); + + it('should provide current user session to custom session id generator', async () => { + const genId = jest.fn().mockImplementation((_req, session) => session.user.nickname); + const baseURL = await setup({ ...config, session: { ...config.session, genId } }); + const cookieJar = await login(baseURL); + const genIdParams = genId.mock.calls.at(0); + expect(genIdParams.length).toEqual(2); + expect('idToken' in genIdParams.at(1)).toBeTruthy(); + expect('user' in genIdParams.at(1)).toBeTruthy(); + expect(cookieJar.getCookieStringSync(baseURL)).toMatch(/^appSession=__test_nickname__\..+/); + }); });