How to use the bcryptjs.genSalt function in bcryptjs

To help you get started, we’ve selected a few bcryptjs examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github hemakshis / Basic-MERN-Stack-App / routes / usersRoute.js View on Github external
errors = {...errors, [field]: 'Passwords do not match'}
        }
    });

    if (Object.keys(errors).length > 0) {
        res.json({ errors });
    } else {
        const newUser = new User({
            name: name,
            username: username,
            email: email,
            password: password
        });

        // Generate the Salt
        bcrypt.genSalt(10, (err, salt) => {
            if(err) return err;
            // Create the hashed password
            bcrypt.hash(newUser.password, salt, (err, hash) => {
                if(err) return err;
                newUser.password = hash;
                // Save the User
                newUser.save(function(err){
                    if(err) return err
                    res.json({ success: 'success' });
                });
            });
        });
    }
});
github luosijie / card / models / user.js View on Github external
UserSchema.pre('save', function(next){

	if (this.isNew) {

		this.meta.createAt = this.meta.updateAt = Date.now();

		var self = this;
		//密码加密存储
		bcrypt.genSalt(10, function(err, salt){
			bcrypt.hash(self.password, salt, function(err, hash){
				self.password = hash;
				console.log(self.password);
				next(); 
			})
		})

	}else{
		this.meta.updateAt = Date.now();
		next(); 
	}

	

})
github bradtraversy / devconnector_2.0 / routes / api / users.js View on Github external
}

      const avatar = gravatar.url(email, {
        s: '200',
        r: 'pg',
        d: 'mm'
      });

      user = new User({
        name,
        email,
        avatar,
        password
      });

      const salt = await bcrypt.genSalt(10);

      user.password = await bcrypt.hash(password, salt);

      await user.save();

      const payload = {
        user: {
          id: user.id
        }
      };

      jwt.sign(
        payload,
        config.get('jwtSecret'),
        { expiresIn: 360000 },
        (err, token) => {
github mjhea0 / node-stripe-charge / _old / src / server / models / user.js View on Github external
User.methods.generateHash = (password, callback) => {
  bcrypt.genSalt(10, (err, salt) => {
    if (err) {
      return next(err);
    }
    bcrypt.hash(password, salt, (error, hash) => {
      if (error) {
        return next(error);
      }
      return callback(error, hash);
    });
  });
};
github codeuino / Social-Platform-Donut / server / schema / user.js View on Github external
UserSchema.pre('save', function(next) {
  var user = this;
  if (!user.isModified('pass')) return next();
  bycrypt.genSalt(10, function(err, salt) {
    if (err) {
      return next(err);
    }
    bycrypt.hash(user.pass, salt, function(err, hash) {
      user.pass = hash;
      next();
    });
  });
});
github carlos-kelly / publications / server / models / user.js View on Github external
userSchema.pre('save', function(next) {
  var user = this;
  if (!user.isModified('password')) return next();

  bcrypt.genSalt(kSaltFactor, function(err, salt) {
    if (err) return next(err);

    bcrypt.hash(user.password, salt, function(err, hash) {
      if (err) return next(err);
      user.password = hash;
      next();
    });
  });
});
github carlos-kelly / publications / server / controllers / user.js View on Github external
exports.create = function(req, res) {
  bcrypt.genSalt(5, function(err, salt) {
    bcrypt.hash(new Date().toLocaleTimeString(), salt, function(err, nameHash) {
      var userModel = new UserModel({
        name: nameHash,
        password: 'password',
        temporary: true
      });

      userModel.save(function(err, user) {
        res.json(user || null);
      });
    });
  });
};
github assemblee-virtuelle / Semantic-Bus / webServices / models / userModel.js View on Github external
User.beforeCreate = function (next) {
  var user = this;
    bcrypt.genSalt(10, function (err, salt) {
      if (err) {
        return next(err);
      }
      bcrypt.hash(user.password, salt, function(err, hash) {
        if (err) {
          return next(err);
        }
        user.password = hash;
        next();
      });
    });
};
github djizco / mern-boilerplate / server / routes / user.js View on Github external
router.put('/password', requireAuth, (req, res) => {
  const { oldPassword, newPassword } = req.body;

  if (req.user.validPassword(oldPassword)) {
    bcrypt.genSalt(10, (err, salt) => {
      if (err) {
        res.status(400).send({ err, message: 'Error updating password' });
      }
      bcrypt.hash(newPassword, salt, (err, hash) => {
        if (err) {
          res.status(400).send({ err, message: 'Error updating password' });
        }
        User.findByIdAndUpdate({ _id: req.user._id }, { password: hash }, err => {
          if (err) {
            res.status(400).send({ err, message: 'Error updating password' });
          }
          res.status(200).send({ message: 'Password successfully updated' });
        });
      });
    });
  } else {

bcryptjs

Optimized bcrypt in plain JavaScript with zero dependencies. Compatible to 'bcrypt'.

MIT
Latest version published 8 years ago

Package Health Score

70 / 100
Full package analysis