Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update session on touch #383

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,9 @@ module.exports = function(connect) {
}

if (session && session.cookie && session.cookie.expires) {
// new session, perform update
updateFields.expires = new Date(session.cookie.expires)
updateFields.session = this.transformFunctions.serialize(session)
} else {
updateFields.expires = new Date(Date.now() + this.ttl * 1000)
}
Expand Down
20 changes: 14 additions & 6 deletions test/legacy-tests.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -525,14 +525,22 @@ describe('legacy tests', () => {
test('test_session_touch', done => {
getNativeDbConnection(async (store, db, collection) => {
const sid = 'test_touch-sid'
// set original session
const data = makeData()
await store.set(sid, data)
const session = await collection.findOne({ _id: sid })
assertSessionEquals(sid, data, session)
await store.touch(sid, session.session)
const session2 = await collection.findOne({ _id: sid })
// Check if both expiry date are different
assert.ok(session2.expires.getTime() > session.expires.getTime())
const sessionDoc = await collection.findOne({ _id: sid })
const session = await store.get(sid)
assertSessionEquals(sid, data, sessionDoc)
// touch with new session
await store.touch(sid, makeData())
// check if expiry dates are different
const session2Doc = await collection.findOne({ _id: sid })
const session2 = await store.get(sid)
assert.ok(session2Doc.expires.getTime() > sessionDoc.expires.getTime())
assert.ok(
new Date(session2.cookie.expires).getTime() >
new Date(session.cookie.expires).getTime()
)
cleanup(store, collection, done)
})
})
Expand Down