Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added feature of saveSession and destroySession for every req instance #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ You can see your session id (sid) into the response header:

See full documentation into _doc_ folder.

## Modifying session data
Whenever modified the req.session data, remember execute `req.saveSession()` immediately, otherwise the newly modified data will be lost at the next time when new request comes in

## Destroy session
Execute `req.destroySession()` to clear the session data in the Redis

## Run Tests

All tests are written in mocha and designed to be run with npm:
Expand All @@ -69,4 +75,4 @@ All tests are written in mocha and designed to be run with npm:

[1]: https://www.npmjs.org/package/restify
[2]: http://redis.io
[3]: https://www.npmjs.org/package/phonegap
[3]: https://www.npmjs.org/package/phonegap
30 changes: 28 additions & 2 deletions lib/restify-session.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,19 +494,45 @@ module.exports = function(config) {
if (!loadErr) {
session.setSessionData(reqSid, data, req, res, next);
} else {
next();
nextFlow();
}
});
} else {
session.createSid(function(createErr, sid) {
if (!createErr) {
session.setSessionData(sid, {}, req, res, next);
} else {
next();
nextFlow();
}
});
}
});

// For the saveSession feature
var fakeRes = {setHeader: function(sidHeader, sid){return;}}
var nextFlow = function(){
// Define the saveSession function for every req instance
if(!req.saveSession || typeof req.saveSession !== 'function'){
req.saveSession = function(cb){
if(this.session && this.session.sid)
sessionManager.setSessionData(this.session.sid, this.session, this, fakeRes, function(){
if(cb && typeof cb === 'function') cb();
});
else if(cb && typeof cb === 'function') cb();
}
}

// Define the destroySession function for every req instance
if(!req.destroySession || typeof req.destroySession !== 'function'){
req.destroySession = function(cb){
if(this.session && this.session.sid)
sessionManager.destroy(this.session.sid, cb);
else if(cb && typeof cb === 'function') cb();
}
}
next();
}

};

return session;
Expand Down