From: Luigi Pinca Date: Mon, 23 Apr 2012 20:43:34 +0000 (+0200) Subject: added rap and 80s rooms and improved readability of signup / login code X-Git-Url: https://git.saalbach.dev/?a=commitdiff_plain;h=a29e49ee22d2e2e3fa3d956b490dbb177eb2c4e6;p=binbsis50.git added rap and 80s rooms and improved readability of signup / login code --- diff --git a/config.js b/config.js index 07c720b..aa052b5 100644 --- a/config.js +++ b/config.js @@ -7,6 +7,6 @@ exports.configure = function() { this.sessionsecret = ''; this.songsinarun = 15; this.threshold = 2; // Edit distance threshold - this.rooms = ["pop", "rock", "mixed"]; + this.rooms = ["pop", "rock", "rap", "80s", "mixed"]; return this; }; diff --git a/package.json b/package.json index e0b413a..e73d119 100644 --- a/package.json +++ b/package.json @@ -18,5 +18,5 @@ "engines": { "node": "0.6.x" }, - "version": "0.3.0-3" + "version": "0.3.0-5" } \ No newline at end of file diff --git a/server.js b/server.js index ab49e0e..5ef5fc5 100644 --- a/server.js +++ b/server.js @@ -71,6 +71,50 @@ http.get("/signup", function(req, res) { res.render("signup", {captchaurl:captcha.toDataURL()}); }); +// Sign up route middlewares +var checkCaptcha = function(req, res, next) { + if (req.form.isValid) { + if (req.session.captchacode !== req.form.captcha) { + var errors = {captcha:['no match']}; + var captcha = new Captcha(); + req.session.captchacode = captcha.getCode(); + return res.render("signup", {errors:errors,captchaurl:captcha.toDataURL()}); + } + next(); + } + else { + var captcha = new Captcha(); + req.session.captchacode = captcha.getCode(); + res.render("signup", {errors:req.form.getErrors(),captchaurl:captcha.toDataURL()}); + } +}; + +var checkUserExists = function(req, res, next) { + var userkey = "user:"+req.form.username; + usersdb.exists(userkey, function(err, data) { + if (data === 1) { // User already exists + var errors = {alert: "A user with name "+req.form.username+" already exists."}; + var captcha = new Captcha(); + req.session.captchacode = captcha.getCode(); + return res.render("signup", {errors:errors,captchaurl:captcha.toDataURL()}); + } + next(); + }); +}; + +var checkEmailExists = function(req, res, next) { + var mailkey = "email:"+req.form.email; + usersdb.exists(mailkey, function(err, data) { + if (data === 1) { // Email already exists + var errors = {alert: "A user with that email already exists."}; + var captcha = new Captcha(); + req.session.captchacode = captcha.getCode(); + return res.render("signup", {errors:errors,captchaurl:captcha.toDataURL()}); + } + next(); + }); +}; + http.post("/signup", form( form.filter("username").trim().required().not(/binb/, "is reserved") @@ -80,68 +124,44 @@ http.post("/signup", .is(/^[A-Za-z0-9]{6,15}$/, "6 to 15 alphanumeric characters required"), form.filter("captcha").required() ), - function(req, res) { - if (req.form.isValid) { - if (req.session.captchacode !== req.form.captcha) { - var errors = {captcha:['no match']}; - var captcha = new Captcha(); - req.session.captchacode = captcha.getCode(); - return res.render("signup", {errors:errors,captchaurl:captcha.toDataURL()}); - } - var userkey = "user:"+req.form.username; - usersdb.exists(userkey, function(err, data) { - if (data === 1) { // User already exists - var errors = {alert: "A user with name "+req.form.username+" already exists."}; - var captcha = new Captcha(); - req.session.captchacode = captcha.getCode(); - return res.render("signup", {errors:errors,captchaurl:captcha.toDataURL()}); - } - var mailkey = "email:"+req.form.email; - usersdb.exists(mailkey, function(e, d) { - if (d === 1) { // Email already exists - var errors = {alert: "A user with that email already exists."}; - var captcha = new Captcha(); - req.session.captchacode = captcha.getCode(); - return res.render("signup", {errors:errors,captchaurl:captcha.toDataURL()}); - } - var salt = ""; - while (salt.length < 8) { - salt += CHARACTERS[Math.floor(Math.random() * CHARACTERS.length)]; - } - var hash = crypto.createHash('sha256') - .update(salt+req.form.password).digest('hex'); - var date = new Date(); - var joindate = date.getDate()+"/"+(date.getMonth()+1)+"/"+date.getFullYear(); - usersdb.hmset(userkey, "username", req.form.username, - "email", req.form.email, - "password", hash, - "salt", salt, - "joindate", joindate, - "totpoints", 0, - "bestscore", 0, - "golds", 0, - "silvers", 0, - "bronzes", 0, - "bestguesstime", 30000, - "worstguesstime", 0, - "totguesstime", 0, - "guessed", 0, - "victories", 0, - "secondplaces", 0, - "thirdplaces", 0); - usersdb.set(mailkey, userkey); - usersdb.sadd("users", userkey); - usersdb.sadd("emails", mailkey); - var msg = "You successfully created your account. You are now ready to login."; - res.render("login", {success:msg}); - }); - }); - } - else { - var captcha = new Captcha(); - req.session.captchacode = captcha.getCode(); - res.render("signup", {errors:req.form.getErrors(),captchaurl:captcha.toDataURL()}); - } + checkCaptcha, + checkUserExists, + checkEmailExists, + function (req, res) { // Set up the account + var userkey = "user:"+req.form.username; + var mailkey = "email:"+req.form.email; + var salt = ""; + while (salt.length < 8) { + salt += CHARACTERS[Math.floor(Math.random() * CHARACTERS.length)]; + } + var hash = crypto.createHash('sha256').update(salt+req.form.password).digest('hex'); + var date = new Date(); + var joindate = date.getDate()+"/"+(date.getMonth()+1)+"/"+date.getFullYear(); + usersdb.hmset( + userkey, + "username", req.form.username, + "email", req.form.email, + "password", hash, + "salt", salt, + "joindate", joindate, + "totpoints", 0, + "bestscore", 0, + "golds", 0, + "silvers", 0, + "bronzes", 0, + "bestguesstime", 30000, + "worstguesstime", 0, + "totguesstime", 0, + "guessed", 0, + "victories", 0, + "secondplaces", 0, + "thirdplaces", 0 + ); + usersdb.set(mailkey, userkey); + usersdb.sadd("users", userkey); + usersdb.sadd("emails", mailkey); + var msg = "You successfully created your account. You are now ready to login."; + res.render("login", {success:msg}); } ); @@ -154,28 +174,14 @@ http.post("/login", form.filter("username").trim().required(), form.filter("password").trim().required() ), - function(req, res) { + function(req, res, next) { if (req.form.isValid) { - var errors = {alert: "The username and/or password you specified are not correct."}; - var key = "user:"+req.form.username; - usersdb.exists(key, function(err, data) { + usersdb.exists("user:"+req.form.username, function(err, data) { if (data === 1) { // User exists - usersdb.hmget(key, "salt", "password", function(e, resp) { - var hash = crypto.createHash('sha256') - .update(resp[0]+req.body.password).digest('hex'); - if (hash === resp[1]) { - req.session.regenerate(function() { - req.session.cookie.maxAge = 604800000; // One week - req.session.user = req.form.username; - res.redirect('/'); - }); - } - else { - res.render("login", {errors:errors}); - } - }); + next(); } else { + var errors = {alert: "The username you specified does not exists."}; res.render("login", {errors:errors}); } }); @@ -183,6 +189,22 @@ http.post("/login", else { res.render("login", {errors:req.form.getErrors()}); } + }, + function(req, res) { // Authenticate User + usersdb.hmget("user:"+req.form.username, "salt", "password", function(err, data) { + var hash = crypto.createHash('sha256').update(data[0]+req.body.password).digest('hex'); + if (hash === data[1]) { + req.session.regenerate(function() { + req.session.cookie.maxAge = 604800000; // One week + req.session.user = req.form.username; + res.redirect('/'); + }); + } + else { + var errors = {alert: "The password you specified is not correct."}; + res.render("login", {errors:errors}); + } + }); } ); diff --git a/views/room.jade b/views/room.jade index bc97dda..d611b37 100644 --- a/views/room.jade +++ b/views/room.jade @@ -67,7 +67,8 @@ html .title Track .track p#feedback Waiting for connection... - input#guess.span8(type="text",placeholder="guess the artist and/or title here") + input#guess.span8(type="text",tabindex="1", + placeholder="guess the artist and/or title here") section.relative .row #users-wrapper.span5.offset2 @@ -81,7 +82,7 @@ html ul#chat.unstyled #message-wrapper span#recipient - input#message.span8(type="text") + input#message.span8(type="text",tabindex="2") ul#tracks.unstyled #disclaimer div I do not own any right on the songs that are played here.