user model cleanup
This commit is contained in:
2
app.js
2
app.js
@ -49,7 +49,7 @@ var app = express();
|
|||||||
|
|
||||||
mongoose.connect(secrets.db);
|
mongoose.connect(secrets.db);
|
||||||
mongoose.connection.on('error', function() {
|
mongoose.connection.on('error', function() {
|
||||||
console.error('MongoDB Connection Error. Make sure MongoDB is running.');
|
console.error('MongoDB Connection Error. Please make sure that MongoDB is running.');
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
var mongoose = require('mongoose');
|
|
||||||
var bcrypt = require('bcrypt-nodejs');
|
var bcrypt = require('bcrypt-nodejs');
|
||||||
var crypto = require('crypto');
|
var crypto = require('crypto');
|
||||||
|
var mongoose = require('mongoose');
|
||||||
|
|
||||||
var userSchema = new mongoose.Schema({
|
var userSchema = new mongoose.Schema({
|
||||||
email: { type: String, unique: true, lowercase: true },
|
email: { type: String, unique: true, lowercase: true },
|
||||||
@ -27,20 +27,19 @@ var userSchema = new mongoose.Schema({
|
|||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hash the password for security.
|
* Password hashing Mongoose middleware.
|
||||||
* "Pre" is a Mongoose middleware that executes before each user.save() call.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
userSchema.pre('save', function(next) {
|
userSchema.pre('save', function(next) {
|
||||||
var user = this;
|
var user = this;
|
||||||
|
|
||||||
if (!user.isModified('password')) return next();
|
if (!user.isModified('password')) { return next(); }
|
||||||
|
|
||||||
bcrypt.genSalt(5, function(err, salt) {
|
bcrypt.genSalt(5, function(err, salt) {
|
||||||
if (err) return next(err);
|
if (err) { return next(err); }
|
||||||
|
|
||||||
bcrypt.hash(user.password, salt, null, function(err, hash) {
|
bcrypt.hash(user.password, salt, null, function(err, hash) {
|
||||||
if (err) return next(err);
|
if (err) { return next(err); }
|
||||||
user.password = hash;
|
user.password = hash;
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
@ -48,24 +47,22 @@ userSchema.pre('save', function(next) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validate user's password.
|
* Helper method for validationg user's password.
|
||||||
* Used by Passport-Local Strategy for password validation.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
userSchema.methods.comparePassword = function(candidatePassword, cb) {
|
userSchema.methods.comparePassword = function(candidatePassword, cb) {
|
||||||
bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
|
bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
|
||||||
if (err) return cb(err);
|
if (err) { return cb(err); }
|
||||||
cb(null, isMatch);
|
cb(null, isMatch);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get URL to a user's gravatar.
|
* Helper method for getting user's gravatar.
|
||||||
* Used in Navbar and Account Management page.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
userSchema.methods.gravatar = function(size) {
|
userSchema.methods.gravatar = function(size) {
|
||||||
if (!size) size = 200;
|
if (!size) { size = 200; }
|
||||||
|
|
||||||
if (!this.email) {
|
if (!this.email) {
|
||||||
return 'https://gravatar.com/avatar/?s=' + size + '&d=retro';
|
return 'https://gravatar.com/avatar/?s=' + size + '&d=retro';
|
||||||
|
Reference in New Issue
Block a user