bcrypt.compare(password, this.password, function(err, isMatch) { if (err) return callback(err); callback(null, isMatch); });
const bcryptOriginal = require('bcrypt-nodejs'); const bcryptNew = require('bcryptjs'); bcryptOriginal.hash('my1Long:password', null, null, function(err, res) { bcryptNew.compare('my1Long:password', res, function(err, res) { expect(res).toBeTruthy();
/** changes password */ const change_password = async ({ password, id }) => { let hash = bcrypt.hashSync(password) let [e] = await catchify( db.query('UPDATE users SET password=? WHERE id=?', [hash, id]) ) return e ? false : true }
/** * compares password * @param {String} password Password * @param {String} hash Hash to be compared with password * @returns {Boolean} Boolean */ const comparePassword = (password, hash) => { let comp = bcrypt.compareSync(password, hash) return comp }
/** * creates a new user * @param {Object} User User details */ const create_user = async user => { let hash = bcrypt.hashSync(user.password) user.password = hash let [e, s] = await catchify(db.query('INSERT INTO users SET ?', user)) e ? console.log(e) : null return s }
User.beforeSave((user, options) => { if (user.changed('password')) { user.password = bcrypt.hashSync(user.password, bcrypt.genSaltSync(10), null); } });
// Hooks are automatic methods that run during various phases of the User Model lifecycle // In this case, before a User is created, we will automatically hash their password User.hook("beforeCreate", function(user) { user.password = bcrypt.hashSync(user.password, bcrypt.genSaltSync(10), null); });
app.post('/signup', (req, res) => { const newUser = { id: uuid.v4(), email: req.body.email, password: bcrypt.hashSync(req.body.password) }; users.push(newUser); req.flash('newUserEmail', newUser.email); req.flash('newUserPassword', newUser.password); res.redirect('success'); });
UserSchema.pre('save', function(next){ var user = this; if(!user.isModified('password')) return next(); bcrypt.hash(user.password, null, null, function(err, hash){ if(err) return next(err); user.password = hash; next(); }); });
bcrypt.compare(candidatePassword, this.password, function (err, isMatch) { if (err) { return cb(err); } cb(null, isMatch); });
//hash the password before the user is saved UserSchema.pre('save', function(next) { var user = this; //hash the password only if the password has been changed or user is new if(!user.isModified('password')) return next(); bcrypt.hash(user.password, null,null,function(err,hash) { if (err) return next(err); //change the password to the hashed version user.password = hash; next(); }); });
bcrypt.compare(pw, this.password, (err, isMatch) => { if (err) { return cb(err); } return cb(null, isMatch); })