Cycymomo
1/18/2014 - 3:13 AM

callbacks.js

/**
 * POST to create a new user.
 */

exports.create = function *(){
  var body = yield parse(this);

  // password
  var pass = body.password;
  assert(pass, 400, 'password is required');
  delete body.password;

  let {salt, hash} = yield password(pass);
  body.password_salt = salt;
  body.password_hash = hash;

  // validate
  users.schema.validate(body);

  // see if the user exists
  var exists = yield users.findOne({ username: body.username }, 'name');
  assert(!exists, 400, 'username is taken');
  
  // save
  yield users.insert(body);

  this.status = 201;
};