Skip to content

Instantly share code, notes, and snippets.

@evantahler
Created November 15, 2015 02:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evantahler/59ba68a5ef5990574b7d to your computer and use it in GitHub Desktop.
Save evantahler/59ba68a5ef5990574b7d to your computer and use it in GitHub Desktop.
actionhero + simple session
////////////////////////////////////////////////////////////////////////////
// Sessions
module.exports = {
initialize: function(api, next){
api.session = {
prefix: "__session",
sessionExipreTime: 1000 * 60 * 60 // 1 hour
};
api.session.fingerprint = function(connection){
if(connection.fingerprint != null){
return connection.fingerprint;
}else{
return conneciton.id;
}
}
api.session.save = function(connection, next){
var key = api.session.prefix + "-" + api.session.fingerprint(connection);
var value = connection.session;
api.cache.save(key, value, api.session.sessionExipreTime, function(err, didSave){
api.cache.load(key, function(err, savedVal){
// console.log(savedVal);
if(typeof next == "function"){ next(err, savedVal); };
});
});
}
api.session.load = function(connection, next){
var key = api.session.prefix + "-" + api.session.fingerprint(connection);
api.cache.load(key, function(err, value){
connection.session = value;
next(err, value);
});
}
next();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment