able to write to the db

This commit is contained in:
Michael Q Larson
2014-10-13 16:01:52 -07:00
parent a5985cd73d
commit ad929be410
9 changed files with 762 additions and 699 deletions

View File

@@ -1,74 +1,28 @@
var mongoose = require('mongoose');
var secrets = require('../config/secrets');
var bcrypt = require('bcrypt-nodejs');
var crypto = require('crypto');
var courses = require('../seed_data/courses.json');
var userSchema = new mongoose.Schema({
email: { type: String, unique: true, lowercase: true },
password: String,
console.log(courses);
twitter: String,
instagram: String,
tokens: Array,
profile: {
name: { type: String, default: '' },
gender: { type: String, default: '' },
location: { type: String, default: '' },
website: { type: String, default: '' },
picture: { type: String, default: '' }
},
resetPasswordToken: String,
resetPasswordExpires: Date
var courseSchema = new mongoose.Schema({
name: { type: String, unique: true },
link: String,
image: String,
time: Number,
directions_1: String,
directions_2: String,
directions_3: String,
});
/**
* Hash the password for security.
* "Pre" is a Mongoose middleware that executes before each user.save() call.
*/
var Courses = module.exports = mongoose.model('Course', courseSchema);
userSchema.pre('save', function(next) {
var user = this;
if (!user.isModified('password')) return next();
bcrypt.genSalt(5, function(err, salt) {
if (err) return next(err);
bcrypt.hash(user.password, salt, null, function(err, hash) {
if (err) return next(err);
user.password = hash;
next();
});
if (require.main === module) {
mongoose.connect(secrets.db);
Courses.create(courses, function(err, data) {
if (err) console.log(err);
else console.log('Saved ', data );
process.exit(0);
});
});
/**
* Validate user's password.
* Used by Passport-Local Strategy for password validation.
*/
userSchema.methods.comparePassword = function(candidatePassword, cb) {
bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
if (err) return cb(err);
cb(null, isMatch);
});
};
/**
* Get URL to a user's gravatar.
* Used in Navbar and Account Management page.
*/
userSchema.methods.gravatar = function(size) {
if (!size) size = 200;
if (!this.email) {
return 'https://gravatar.com/avatar/?s=' + size + '&d=retro';
}
var md5 = crypto.createHash('md5').update(this.email).digest('hex');
return 'https://gravatar.com/avatar/' + md5 + '?s=' + size + '&d=retro';
};
module.exports = mongoose.model('User', userSchema);
}