Skip to content

Instantly share code, notes, and snippets.

@mulderp
Created October 11, 2013 20:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mulderp/6941704 to your computer and use it in GitHub Desktop.
Save mulderp/6941704 to your computer and use it in GitHub Desktop.
User signup with Node and Redis
function createUser(raw, success_cb, fail_cb) {
var userId;
console.log("enter");
async.waterfall([
function(cb) {
// Increase
client.hincrby('users', 'count', 1, cb);
},
function(id, cb) {
// Higher Scope a userId variable for access later.
userId = id;
console.log(userId);
// Set
client.hmset('user:'+id,
'username', raw.username,
'password', raw.password,
'email', raw.email,
cb);
},
function(write, cb) {
client.hmget('user:'+userId, 'username', 'email', cb);
}
], function(err,read){
if (err) {
fail_cb(err);
}
success_cb(userId, read);
})
};
@mulderp
Copy link
Author

mulderp commented Oct 12, 2013

function createUser(raw, success_cb, fail_cb) {

 var userId;

 client.incrAsync('users.count').then(function(id){
        userId = id;
        return client.hmsetAsync(
            'user:' + id,
            'username', raw.username,
            'password', raw.password,
            'email', raw.email
        );
      }).then(function() {
         client.setAsync("username.to.id:" + raw.username.toLowerCase(), userId);
      }).then(function(){
        return client.hmgetAsync('user:'+userId, 'username', 'email');
    }).then(function(data) {
      success_cb(userId, data);
    }).catch(function(err){
      fail_cb(err);
    });
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment