Skip to content

Instantly share code, notes, and snippets.

@diego3g
Created May 29, 2018 13:09
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 diego3g/8ef8d147e414db8cccb600af01578a41 to your computer and use it in GitHub Desktop.
Save diego3g/8ef8d147e414db8cccb600af01578a41 to your computer and use it in GitHub Desktop.
// models/user.js
// ...
UserSchema.pre("save", async function hashPassword(next) {
if (!this.isModified("password")) next();
this.password = await bcrypt.hash(this.password, 8);
});
UserSchema.methods = {
compareHash(hash) {
return bcrypt.compare(hash, this.password);
},
generateToken() {
return jwt.sign({ id: this.id }, "secret", {
expiresIn: 86400
});
}
};
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment