From 51842a32dada995ad3e3fa2c3167cb70574d448c Mon Sep 17 00:00:00 2001 From: Baris Usakli Date: Mon, 24 Jun 2013 16:16:50 -0400 Subject: [PATCH] dont let users with the same username/slug register closes #33 --- public/src/forum/register.js | 19 ++++++++++++++++--- src/user.js | 16 ++++++++++++---- src/websockets.js | 2 +- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/public/src/forum/register.js b/public/src/forum/register.js index 2112f1ec37..2245b96ef8 100644 --- a/public/src/forum/register.js +++ b/public/src/forum/register.js @@ -5,15 +5,18 @@ emailEl = document.getElementById('email'), username_notify = document.getElementById('username-notify'), email_notify = document.getElementById('email-notify'), - password_notify = document.getElementById('password-notify'); + password_notify = document.getElementById('password-notify'), + emailexists = false, + userexists = false; - username.onkeyup = function() { + $(username).on('keyup change', function() { if (username.value.length > 2) socket.emit('user.exists', {username: username.value}); else { username_notify.innerHTML = 'Username too short'; username_notify.className = 'label label-important'; } - } + }); + emailEl.addEventListener('change', function() { socket.emit('user.email.exists', { email: emailEl.value }); }, false); @@ -28,6 +31,7 @@ ajaxify.register_events(['user.exists', 'user.email.exists']); socket.on('user.exists', function(data) { + userexists = data.exists; if (data.exists == true) { username_notify.innerHTML = 'Username exists'; username_notify.className = 'label label-important'; @@ -37,6 +41,7 @@ } }); socket.on('user.email.exists', function(data) { + emailexists = data.exists; if (data.exists === true) { email_notify.innerHTML = 'Email Address exists'; } else { @@ -80,6 +85,14 @@ email_notify.innerHTML = ''; } + if(emailexists) { + email_notify.innerHTML = 'Email Address exists'; + validated = false; + } + + if(userexists) + validated = false; + return validated; } register.addEventListener('click', function(e) { diff --git a/src/user.js b/src/user.js index 568cbc4634..57964038c3 100644 --- a/src/user.js +++ b/src/user.js @@ -163,12 +163,20 @@ var utils = require('./../public/src/utils.js'), User.create = function(username, password, email, callback) { - User.exists(username, function(exists) { + var userslug = utils.slugify(username); + + User.exists(userslug, function(exists) { + + if(exists) { + console.log("user name taken"); + callback(null, 0); + return; + } + RDB.incr('global:next_user_id', function(err, uid) { RDB.handle(err); var gravatar = User.createGravatarURLFromEmail(email); - var userslug = utils.slugify(username); RDB.hmset('user:'+uid, { 'username' : username, @@ -380,8 +388,8 @@ var utils = require('./../public/src/utils.js'), }); } - User.exists = function(username, callback) { - User.get_uid_by_username(username, function(exists) { + User.exists = function(userslug, callback) { + User.get_uid_by_userslug(userslug, function(exists) { exists = !!exists; if (callback) diff --git a/src/websockets.js b/src/websockets.js index a81dcfdc83..a3f6886ab5 100644 --- a/src/websockets.js +++ b/src/websockets.js @@ -147,7 +147,7 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }), }); socket.on('user.exists', function(data) { - user.exists(data.username, function(exists){ + user.exists(utils.slugify(data.username), function(exists){ socket.emit('user.exists', {exists: exists}); }); });