Permalink
Browse files

Switched to using Redis for the session store so that sessions persis…

…t when node.js is restarted
  • Loading branch information...
1 parent 7dbea0d commit 681f3efe2d2dfbf8ed92f57475b3b7596f03ceb5 @dangrossman committed Jul 20, 2012
Showing with 14 additions and 10 deletions.
  1. +1 −0 package.json
  2. +13 −10 server.js
View
@@ -10,5 +10,6 @@
,"express": "2.5.6"
,"formidable":"1.0.8"
,"mysql":"0.9.5"
+ ,"connect-redis":"1.4.0"
}
}
View
@@ -10,6 +10,8 @@ var express = require('express'),
parser = require('uglify-js').parser,
uglifyer = require('uglify-js').uglify;
+var RedisStore = require('connect-redis')(express);
+
/* ==============================================================
Configuration
=============================================================== */
@@ -25,9 +27,9 @@ client.database = 'bookmarks';
var app = express.createServer();
-app.use(express.cookieParser());
-app.use(express.session({ secret: salt, cookie: { maxAge: 3600000 * 24 * 30 } }));
app.use(express.bodyParser());
+app.use(express.cookieParser());
+app.use(express.session({ secret: salt, store: new RedisStore, cookie: { maxAge: 3600000 * 24 * 30 } }));
app.use(express.methodOverride());
app.use(express.logger({ format: ':method :url' }));
@@ -152,10 +154,11 @@ app.post('/json/register', function(req, res) {
res.writeHead(200, { 'Content-Type': 'application/javascript' });
res.write(JSON.stringify(user), 'utf8');
- res.end('\n');
req.session.user_id = user.id;
req.session.user = user;
+
+ res.end('\n');
}
});
@@ -171,30 +174,30 @@ app.post('/json/login', function(req, res) {
if (err) console.log(err);
if (results && results.length == 1) {
+ req.session.user_id = results[0].id;
+ req.session.user = results[0];
+
res.setHeader('Cache-Control', 'max-age=0, must-revalidate, no-cache, no-store');
res.writeHead(200, { 'Content-Type': 'application/javascript' });
res.write(JSON.stringify(results[0]), 'utf8');
res.end('\n');
-
- req.session.user_id = results[0].id;
- req.session.user = results[0];
-
+
client.query('UPDATE users SET last_login = CURRENT_TIMESTAMP WHERE id = ?', [req.session.user_id]);
} else {
+ req.session.destroy();
res.writeHead(401, { 'Content-Type': 'text/html' });
res.end();
- req.session.destroy();
}
});
});
//Log out the current user
app.post('/json/logout', function(req, res) {
-
+
+ req.session.destroy();
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end();
- req.session.destroy();
});

0 comments on commit 681f3ef

Please sign in to comment.