var Q = require('q');
var mongoose = require('mongoose');
var bcrypt = require('bcrypt-nodejs');
var SALT_WORK_FACTOR = 10;


var UserSchema = new mongoose.Schema({
  username: {
    type: String,
    required: true,
    unique: true
  },

  password: {
    type: String,
    required: true
  },
  salt: String
});

UserSchema.methods.comparePasswords = function (candidatePassword) {
  var savedPassword = this.password;
  return Q.Promise(function (resolve, reject) {
    bcrypt.compare(candidatePassword, savedPassword, function (err, isMatch) {
      if (err) {
        reject(err);
      } else {
        resolve(isMatch);
      }
    });
  });
};

UserSchema.pre('save', function (next) {
  var user = this;

  // only hash the password if it has been modified (or is new)
  if (!user.isModified('password')) {
    return next();
  }

  // generate a salt
  bcrypt.genSalt(SALT_WORK_FACTOR, function (err, salt) {
    if (err) {
      return next(err);
    }

    // hash the password along with our new salt
    bcrypt.hash(user.password, salt, null, function (err, hash) {
      if (err) {
        return next(err);
      }

      // override the cleartext password with the hashed one
      user.password = hash;
      user.salt = salt;
      next();
    });
  });
});

module.exports = mongoose.model('users', UserSchema);

@@ -171,20 +171,16 @@
 rname: %7B
-%0A   
  type: S
@@ -177,36 +177,92 @@
  %7B type: String,
-%0A   
+ required: true, unique: true %7D,%0A  password: %7B type: String,
  required: true,
@@ -264,103 +264,605 @@
 true
+ %7D
 ,%0A  
-  uniqu
+salt: String,%0A%0A  firstName: %7B typ
 e: 
+S
 tr
-ue%0A  %7D,%0A%0A  password: %7B%0A    type: String,%0A    required: true%0A  %7D,%0A  salt: String
+ing, default: %22Speedee%22 %7D,%0A  lastName: %7B type: String, default: %22Gonzales%22 %7D,%0A  preferedDistance: Number,%0A  mileSpeed: %7B type: Number, default: 10 %7D, // in min/mile%0A  runs: Array%0A  // holds run objects, format should be %7B%0A  //   date: Date,%0A  //   startLocation: %7B%0A  //     longitude: String,%0A  //     latitude: String%0A  //   %7D,%0A  //   endLocation: %7B%0A  //     longitude: String,%0A  //     latitude: String%0A  //   %7D,%0A  //   googleExpectedTime: Number,%0A  //   actualTime: Number,%0A  //   medalRecieved: String,%0A  //   racedAgainst: ObjectID? String?%0A  // %7D
 %0A%7D);

Update user schema