Skip to content

Instantly share code, notes, and snippets.

@reharik
Created April 12, 2015 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reharik/79edabb2e5877e5177f4 to your computer and use it in GitHub Desktop.
Save reharik/79edabb2e5877e5177f4 to your computer and use it in GitHub Desktop.
co.wrap(function*() {
try {
var salt = yield bcrypt.genSalt();
var hash = yield bcrypt.hash(this.password, salt);
this.password = hash;
done();
}
catch (err) {
done(err);
}
}).call(this).then(done);
});
/**
* Methods
*/
UserSchema.methods.comparePassword = function *(candidatePassword) {
return yield bcrypt.compare(candidatePassword, this.password);
};
/**
* Statics
*/
UserSchema.statics.passwordMatches = function *(username, password) {
var user = yield this.findOne({ 'username': username.toLowerCase() }).exec();
if (!user) throw new Error('User not found');
if (yield user.comparePassword(password)) {
return user;
}
throw new Error('Password does not match');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment