Don't put generate() method on store object per request, refactored Session.regenerate() implementation #263

Closed
wants to merge 1 commit into
from
View
@@ -261,13 +261,20 @@ function session(options){
}
// generates the new session
- var generate = store.generate = function(){
+ var generate = (function _generate(){
devijvers
devijvers Apr 23, 2011

Required to allow anyomous method to call itself.

var base = utils.uid(24);
var sessionID = base + '.' + hash(base);
req.sessionID = sessionID;
req.session = new Session(req);
+ var session_regenerate = Session.prototype.regenerate;
+ req.session.__proto__.regenerate = function(fn) {
devijvers
devijvers Apr 23, 2011

The Session.regenerate() method is decorated to call the generate() method in the callback of the destroy() method of the Store.

+ return session_regenerate.call(this, function(err) {
+ _generate();
devijvers
devijvers Apr 23, 2011

Anonymous method calls itself here.

+ fn(err);
+ });
+ };
req.session.cookie = new Cookie(cookie);
- };
+ });
// get the sessionID from the cookie
req.sessionID = req.cookies[key];
@@ -317,6 +324,13 @@ function session(options){
if ('string' == typeof expires) sess.cookie.expires = new Date(expires);
sess.cookie.originalMaxAge = orig;
req.session = new Session(req, sess);
+ var session_regenerate = Session.prototype.regenerate;
+ req.session.__proto__.regenerate = function(fn) {
+ return session_regenerate.call(this, function(err) {
+ generate();
+ fn(err);
+ });
+ };
devijvers
devijvers Apr 23, 2011

Same mechanism required here since new Session object is created.

req.session.resetLastAccess();
next();
}
@@ -32,7 +32,6 @@ var Store = module.exports = function Store(options){};
Store.prototype.regenerate = function(req, fn){
var self = this;
this.destroy(req.sessionID, function(err){
- self.generate();
fn(err);
});
-};
+};