Skip to content

Commit

Permalink
[SDK-3717] Add cookie prop to support more express-session stores (#395)
Browse files Browse the repository at this point in the history
Add cookie prop to support more express-session stores
  • Loading branch information
adamjmcgrath authored Oct 17, 2022
1 parent db310d4 commit b90c6fe
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/appSession.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ module.exports = (config) => {
await this._set(req[REGENERATED_SESSION_ID] || id, {
header: { iat, uat, exp },
data: req[sessionName],
cookie: {
expires: exp * 1000,
maxAge: exp * 1000 - Date.now(),
},
});
}
}
Expand Down
21 changes: 21 additions & 0 deletions test/appSession.customStore.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ describe('appSession custom store', () => {
redisClient.asyncSet = promisify(redisClient.set).bind(redisClient);
redisClient.asyncGet = promisify(redisClient.get).bind(redisClient);
redisClient.asyncDbsize = promisify(redisClient.dbsize).bind(redisClient);
redisClient.asyncTtl = promisify(redisClient.ttl).bind(redisClient);

const conf = getConfig({
...defaultConfig,
Expand Down Expand Up @@ -118,6 +119,26 @@ describe('appSession custom store', () => {
});
});

it('should set ttl for compatible session stores', async () => {
const twoDays = 172800;
await setup({ session: { rolling: false, absoluteDuration: twoDays } });
await redisClient.asyncSet('foo', sessionData());
const jar = request.jar();
const res = await request.get('/session', {
baseUrl,
jar,
json: true,
headers: {
cookie: 'appSession=foo',
},
});
assert.equal(res.statusCode, 200);
assert.deepEqual(res.body, { sub: '__test_sub__' });
assert.equal(res.statusCode, 200);
const ttl = await redisClient.asyncTtl('foo');
assert.closeTo(ttl, twoDays, 10 * 1000);
});

it('should not populate the store when there is no session', async () => {
await setup();
await request.get('/session', {
Expand Down

0 comments on commit b90c6fe

Please sign in to comment.