Skip to content

Commit

Permalink
feat: added ctx.saveSession method
Browse files Browse the repository at this point in the history
  • Loading branch information
niftylettuce committed Dec 23, 2019
1 parent 2ab0a9c commit 5205899
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ app.listen(8080);
* Setting `this.session = null;` will destroy this session.
* Altering `this.session.cookie` changes the cookie options of this user. Also you can use the cookie options in session the store. Use for example `cookie.maxAge` as the session store's ttl.
* Calling `this.regenerateSession` will destroy any existing session and generate a new, empty one in its place. The new session will have a different ID.
* Calling `this.saveSession` will save an existing session (this method was added for [koa-redirect-loop](https://github.com/niftylettuce/koa-redirect-loop))
* Setting `this.sessionSave = true` will force saving the session regardless of any other options or conditions.
* Setting `this.sessionSave = false` will prevent saving the session regardless of any other options or conditions.

Expand Down
19 changes: 19 additions & 0 deletions src/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,14 @@ module.exports = function(options = {}) {
}
})

ctx.saveSession = async function saveSession() {
const result = await getSession(ctx)
if (!result) {
return next()
}
return refreshSession(ctx, ctx.session, result.originalHash, result.isNew)
}

ctx.regenerateSession = async function regenerateSession() {
debug('regenerating session')
if (!result.isNew) {
Expand Down Expand Up @@ -416,6 +424,17 @@ module.exports = function(options = {}) {
// internal flag to determine that session is already defined
ctx.__isSessionDefined = true

ctx.saveSession = async function saveSession() {
// make sure that the session has been loaded
await ctx.session

const result = await getSession(ctx)
if (!result) {
return next()
}
return refreshSession(ctx, ctx.session, result.originalHash, result.isNew)
}

ctx.regenerateSession = async function regenerateSession() {
debug('regenerating session')
// make sure that the session has been loaded
Expand Down

0 comments on commit 5205899

Please sign in to comment.