Skip to content

Instantly share code, notes, and snippets.

@mulderp mulderp/createUser
Created Oct 11, 2013

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

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
You can’t perform that action at this time.