Created
April 12, 2015 16:06
-
-
Save reharik/79edabb2e5877e5177f4 to your computer and use it in GitHub Desktop.
This file contains 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
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