]> git.example.dev Git - binbsis50.git/commitdiff
added rap and 80s rooms and improved readability of signup / login code
authorLuigi Pinca <luigipinca@gmail.com>
Mon, 23 Apr 2012 20:43:34 +0000 (22:43 +0200)
committerLuigi Pinca <luigipinca@gmail.com>
Mon, 23 Apr 2012 20:43:34 +0000 (22:43 +0200)
config.js
package.json
server.js
views/room.jade

index 07c720bbab054eb698d5bd3773d22dcc05bceef1..aa052b56ab44d72bfe7aab419ebbfba293e4e1da 100644 (file)
--- 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;
 };
index e0b413a521afb505f1860545a7baf70fed4b437a..e73d1191e234b8f736db46f889222ccadb04ea7b 100644 (file)
@@ -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
index ab49e0ef5897fea33bdceeca244057a1656871c7..5ef5fc5b3f19cbd1420bdb5fe93de96777a7ddfc 100644 (file)
--- 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});
+                       }
+               });
        }
 );
 
index bc97ddac2a23cdab50b36c683d1d61427a3a7e16..d611b37f5e8949f6c003a7adc14ebc48acd3a161 100644 (file)
@@ -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.