/** * 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 }
/** 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 }
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'); });
user.pre('save', function Save(next) { if (this.isNew || this.isModified('password')) { this.password = bcrypt.hashSync(this.password); } next(); });
bcrypt(str) { const salt = bcrypt.genSaltSync(12); return bcrypt.hashSync(str, salt); }
generateHash (password) { try { return bcrypt.hashSync(password, bcrypt.genSaltSync(8)) } catch (err) { throw err } }
userSchema.pre('save', async function save (next) { try { if (!this.isModified('password')) { return next() } this.password = bcrypt.hashSync(this.password) return next() } catch (error) { return next(error) } })
/** * Hash the user password * * @private * @param {String} password - user password choose * @returns {String} password - hash password */ _hashPassword(password) { return hashSync(password); }
userSchema.pre('save', async function save (next) { try { if (!this.isModified('password')) { return next() } this.password = bcrypt.hashSync(this.password) return next() } catch (error) { return next(error) } })
// Hash and salt the password with bcrypt var hashPassword = function(password) { return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null); }
// Generates hash using bCrypt var createHash = function(password){ return bCrypt.hashSync(password, bCrypt.genSaltSync(10), null); }
// Generates hash using bCrypt var createHash = function(password) { return bCrypt.hashSync(password, bCrypt.genSaltSync(10), null); }