function createUser(username, password, email, callback) {
  async.parallel([
    function(next) {

      // Check if newUser.username is already in UserMongoModel
      UserMongoModel.findOne({ usernameLower : username.toLowerCase() },
                     function(err, user) {
        if (user) {
          next({
            code    : 400,
            message : 'Username already exists'
          });
        } else {
          next(null);
        }
      });
    }, function(next) {

      // Check if newUser.email is already in UserMongoModel
      UserMongoModel.findOne({ email : email.toLowerCase() },
                             function(err, user) {
        if (user) {
          next({
            code    : 400,
            message : 'Email already exists'
          });
        } else {
          next(null);
        }
      });
    }
  ], function(err) {
    if (err) {
      callback(err);
    } else {

      async.waterfall([
        function(next) {

          bcrypt.hash(password, BCRYPT_SALT_ROUNDS, function(err, hash) {
            if (err) {
              next(err);
            } else {
              next(null, hash);
            }
          });

        }, function(hash, next) {
          UserMongoModel.create({
            username      : username,
            usernameLower : username.toLowerCase(),
            email         : email.toLowerCase(),
            password      : hash
          }, function(err, user) {
            if (err) {

              // TODO: Error message?
              next(err);
            } else {
              next(null, user._id);
            }
          });

        }
      ], function(err, userId) {
        if (err) {
          callback(err);
        } else {
          Session.create(userId, callback);
        }
      });
    }
  });
}
// Takes username, password, email and saves a new user to MongoDB
Takes username, password, email and saves a new user to MongoDB
Takes username password email and saves a new user to MongoDB
function createUser(username, password, email, callback) {
  async.parallel([
    function(next) {
      UserMongoModel.findOne({ usernameLower : username.toLowerCase() },
                     function(err, user) {
        if (user) {
          next({
            code    : 400,
            message : 'Username already exists'
          });
        } else {
          next(null);
        }
      });
    }, function(next) {
      UserMongoModel.findOne({ email : email.toLowerCase() },
                             function(err, user) {
        if (user) {
          next({
            code    : 400,
            message : 'Email already exists'
          });
        } else {
          next(null);
        }
      });
    }
  ], function(err) {
    if (err) {
      callback(err);
    } else {
      async.waterfall([
        function(next) {
          bcrypt.hash(password, BCRYPT_SALT_ROUNDS, function(err, hash) {
            if (err) {
              next(err);
            } else {
              next(null, hash);
            }
          });
        }, function(hash, next) {
          UserMongoModel.create({
            username      : username,
            usernameLower : username.toLowerCase(),
            email         : email.toLowerCase(),
            password      : hash
          }, function(err, user) {
            if (err) {
              next(err);
            } else {
              next(null, user._id);
            }
          });
        }
      ], function(err, userId) {
        if (err) {
          callback(err);
        } else {
          Session.create(userId, callback);
        }
      });
    }
  });
}
function createUser ( username , password , email , callback ) { async . parallel ( [ function ( next ) { UserMongoModel . findOne ( { usernameLower : username . toLowerCase ( ) } , function ( err , user ) { if ( user ) { next ( { code : 400 , message : 'Username already exists' } ) ; } else { next ( null ) ; } } ) ; } , function ( next ) { UserMongoModel . findOne ( { email : email . toLowerCase ( ) } , function ( err , user ) { if ( user ) { next ( { code : 400 , message : 'Email already exists' } ) ; } else { next ( null ) ; } } ) ; } ] , function ( err ) { if ( err ) { callback ( err ) ; } else { async . waterfall ( [ function ( next ) { bcrypt . hash ( password , BCRYPT_SALT_ROUNDS , function ( err , hash ) { if ( err ) { next ( err ) ; } else { next ( null , hash ) ; } } ) ; } , function ( hash , next ) { UserMongoModel . create ( { username : username , usernameLower : username . toLowerCase ( ) , email : email . toLowerCase ( ) , password : hash } , function ( err , user ) { if ( err ) { next ( err ) ; } else { next ( null , user . _id ) ; } } ) ; } ] , function ( err , userId ) { if ( err ) { callback ( err ) ; } else { Session . create ( userId , callback ) ; } } ) ; } } ) ; }
Takes username, password, email and saves a new user to MongoDB
Takes username password email and saves a new user to MongoDB
// Check if newUser.username is already in UserMongoModel // Check if newUser.email is already in UserMongoModel // TODO: Error message?