Created
August 6, 2011 23:45
-
-
Save iamwilhelm/1129891 to your computer and use it in GitHub Desktop.
asynchronous flow control
This file contains 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
app.post '/users/signup', (req, res) -> | |
crypted_password = User.generate_crypted_password req.body.user.password | |
user = User.findOne { "facebook.uid": req.body.uid } | |
if user | |
user.email = req.body.user.email | |
user.password = crypted_password | |
else | |
user = new User({ email: req.body.user.email, password: crypted_password }) | |
user.save | |
req.session.regenerate | |
req.flash 'info', "Welcome into the fold!" | |
res.redirect 'hospitality/welcome' |
This file contains 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
app.post '/users/signup', (req, res) -> | |
User.generate_crypted_password req.body.user.password, (err, crypted_password) -> | |
if err | |
req.flash 'error', "Could not generate crypted password" | |
res.redirect '/' | |
User.findOne { "facebook.uid": req.body.uid }, (err, user) -> | |
if err | |
req.flash 'error', "There was an error finding user" | |
res.redirect '/' | |
if user | |
user.email = req.body.user.email | |
user.password = crypted_password | |
else | |
user = new User({ email: req.body.user.email, crypted_password: crypted_password }) | |
user.save (err) -> | |
if err | |
# TODO use error object to generate flash message | |
req.flash 'error', "Couldn't save your new account" | |
res.redirect '/users/signup' | |
else | |
req.session.regenerate () -> | |
req.session.user_id = user.id | |
req.flash 'info', "Welcome into the fold!" | |
res.redirect 'hospitality/welcome' |
This file contains 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
app.post '/users/signup', (req, res) -> | |
async.waterfall [ | |
(cb) -> | |
User.generate_crypted_password req.body.user.password, (err, user) -> | |
cb(err, user) | |
, | |
(cryptedPassword, cb) -> | |
User.findOne { "facebook.uid": req.body.uid }, (err, user) -> | |
cb(err, user, cryptedPassword) | |
, | |
(user, cryptedPassword, cb) -> | |
if user | |
user.email = req.body.user.email | |
user.password = cryptedPassword | |
console.log "Found Facebook user to use" | |
else | |
user = new User { email: req.body.user.email, crypted_password: cryptedPassword } | |
console.log "Created new user to register" | |
cb(null, user) | |
, | |
(user, cb) -> | |
user.save (err) -> | |
cb(err, user) | |
, | |
], (err, user) -> | |
if err | |
console.log err | |
console.log results | |
req.flash 'error', "Couldn't save your new account" | |
res.redirect '/users/signup' | |
else | |
req.session.regenerate () -> | |
req.session.user_id = user.id | |
req.flash 'info', "Welcome into the fold!" | |
res.redirect 'hospitality/welcome' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment