Created
October 4, 2012 10:34
-
-
Save FrancescaK/3832833 to your computer and use it in GitHub Desktop.
Password Testing Scenario
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoose = require(‘mongoose’), | |
Schema = mongoose.Schema, | |
bcrypt = require(‘bcrypt’), | |
SALT_WORK_FACTOR = 10; | |
var UserSchema = new Schema({ | |
username: { type: String, required: true, index: { unique: true } }, | |
password: { type: String, required: true } | |
}); | |
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 using our new salt | |
bcrypt.hash(user.password, salt, function(err, hash) { | |
if (err) return next(err); | |
// override the cleartext password with the hashed one | |
user.password = hash; | |
next(); | |
}); | |
}); | |
}); | |
UserSchema.methods.comparePassword = function(candidatePassword, cb) { | |
bcrypt.compare(candidatePassword, this.password, function(err, isMatch) { | |
if (err) return cb(err); | |
cb(null, isMatch); | |
}); | |
}; | |
module.exports = mongoose.model(User, UserSchema); |
It should be 'save' not save
Thank you for the corrected version!
Also, line 41 should be
module.exports = mongoose.model('User', UserSchema);
Refined code snippet:
https://gist.github.com/arekgofi/46581f3aec2661465b2a447fb8a39e78
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What is
save
? https://gist.github.com/3832833#file-gistfile1-txt-L11