Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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' });
});
});
});
}
});
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();
}
})
}
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) => {
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);
});
});
};
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();
});
});
});
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();
});
});
});
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);
});
});
});
};
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();
});
});
};
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 {