express-new-session
v1.0.0
Published
A middleware that generates a new session.
Downloads
7
Maintainers
Readme
express-new-session
This middleware generates a new session.
What's that?
It calls the req.session.regenerate method of express-session for all methods except GET. It provides a totally new session with different id.
There's a way to specify a list of keys that are going to be preserved. It makes it possible to obtain a new session that contains some of the data that was stored within the old session.
Examples
The most complex example looks like this:
const newSession = require('express-new-session')
//...
const preservedKeys = ["fontSize", "helpClosed"]
app.all('/login', newSession({preservedKeys}), (req, res) => {
//...
})
It does nothing, if the /login page is just displayed, because GET requests are simply ignored by this middleware. But in every other case, it generates a new session before control is passed to the next middleware. Two session variables are preserved: fontSize and helpClosed. These are the only variables that will be available to the second middleware within the req.session variable.
Options are optional, so if we don't need to preserve any session keys, we can skip them:
app.all('/login', newSession(), (req, res) => {
//...
})
How to get it?
$ npm i express-new-session --save