Is there a way to control when a session starts with session middleware?
For example, if I have an express config application:
var app = express(); app.configure(function(){ app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(express.cookieParser('secret')); app.use(express.session({ store:sessionStore, ... })); });
Then, with each request, if no cookie session is specified, the session begins. What if I wanted to start a session only after user authentication?
For example, let's say I have two routes /protected and /login .
- If someone clicked
/protected without a session cookie, the middleware will NOT start a new session. ( req.session - null ) - If someone clicked
/protected with a session cookie, the middleware will CHECK to see if there is a corresponding active session for the cookie and req.session set, but a new session will not start, ( req.session may be or null ) - If someone clicked on
/login with the correct parameters, then the session starts explicitly , and the cookie is set only then.
The only way to start a session should be explicit:
app.post('/login', function(req, res, next) {
Is there a way to accomplish this using the existing session middleware?
ralouphie
source share