Created
October 11, 2013 20:41
-
-
Save mulderp/6941704 to your computer and use it in GitHub Desktop.
User signup with Node and Redis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| }) | |
| }; | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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); }); };