Created
May 29, 2018 13:09
-
-
Save diego3g/8ef8d147e414db8cccb600af01578a41 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
// 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