From 5205899c49ba94d2563f2654e25f0eab6e7eb5be Mon Sep 17 00:00:00 2001 From: niftylettuce Date: Mon, 23 Dec 2019 05:36:54 -0600 Subject: [PATCH] feat: added ctx.saveSession method --- README.md | 1 + src/session.js | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/README.md b/README.md index 4003c93..0454673 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/session.js b/src/session.js index a3aa9a6..735031a 100755 --- a/src/session.js +++ b/src/session.js @@ -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) { @@ -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