Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
module.exports.createNgo = (newNgo, callback)=>{
bcrypt.genSalt(10, function(err, salt) {
bcrypt.hash(newNgo.password, salt, function(err, hash) {
// Store hash in your password DB.
newNgo.password = hash
// console.log(hash)
newNgo.save(callback)
});
});
};
UserSchema.pre('save', function(next) {
const user = this;
// only hash the password if it has been modified (or is new)
if (!user.isModified('password')) {
return next();
}
// generate a salt
bcrypt.genSalt(WORK_FACTOR, function (err, salt) {
if (err) return next(err);
// hash the password along with our new salt
bcrypt.hash(user.password, salt, function (err, hash) {
if (err) return next(err);
// override the cleartext password with the hashed one
user.password = hash;
// let mongoose know we're done now that we've hashed the plaintext password
next();
});
});
});
UserSchema.pre("save", function(next) {
// if the password Field on our schema is not modified we want to return next
if (!this.isModified("password")) {
return next();
}
bcrypt.genSalt(10, (err, salt) => {
if (err) return next(err);
bcrypt.hash(this.password, salt, (err, hash) => {
if (err) return next(err);
this.password = hash;
next();
});
});
});
UserSchema.pre('save', function(next) {
let user = this;
if (this.isModified('password') || this.isNew) {
bcrypt.genSalt(10, (err, salt) => {
if (err) {
console.log(err);
return next(err);
}
bcrypt.hash(user.password, salt, (err, hash) => {
if (err) {
console.log(err);
return next(err);
}
user.password = hash;
next();
});
});
} else {
AM.saltAndHash = function(pass, callback)
{
bcrypt.genSalt(10, function(err, salt) {
bcrypt.hash(pass, salt, function(err, hash) {
callback(hash);
});
});
}
function hashPassword (password, next) {
bcrypt.genSalt(10, function (err, salt) {
if (err) { return next(err); }
bcrypt.hash(password, salt, next);
});
}
hashPassword(cb) {
bcrypt.genSalt(12, (err, salt) => {
if (err) return cb(err);
this.salt = salt;
bcrypt.hash(this.pass, salt, (err, hash) => {
if (err) return cb(err);
this.pass = hash;
cb();
});
});
}
return new Promise((resolve, reject) => {
Bcrypt.genSalt(10, (err, salt) => {
Bcrypt.hash(user.password, salt, (err, hash) => {
const q = 'UPDATE `users` SET `password` = ? WHERE `id` = ?;';
const a = [hash, user.id];
this.db.query(q, a).then(resolve).catch(reject);
});
});
});
}
async registerUser(user: User): Promise {
const actualUser = await this.findOne(user.username!);
if (actualUser) {
throw new Error('User already exists');
}
const salt = await genSalt(12);
const hashPass = await hash(user.password, salt);
return plainToClass(
User,
await this.userRepository.save({
username: user.username,
password: hashPass,
role: roles.USER,
}),
);
}
}
public generateSalt(
rounds: number = this.api.configs.general.saltRounds,
): Promise {
return bcrypt.genSalt(rounds);
}