Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- userSchema.pre('save', function (next) {
- const user = this;
- if (this.isModified('password') || this.isNew) {
- bcrypt.genSalt(10, (err, salt) => {
- if (err) {
- return next(err);
- }
- bcrypt.hash(user.password, salt, (err, hash) => {
- if (err) {
- return next(err);
- }
- user.password = hash;
- next();
- });
- });
- } else {
- return next();
- }
- });
- // Compare password input to password saved in database
- userSchema.methods.comparePassword = function (pw, cb) {
- bcrypt.compare(pw, this.password, (err, isMatch) => {
- if (err) {
- return cb(err);
- }
- cb(null, isMatch);
- });
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement