I am running some project on MEAN.js and I have the following problem. I want to do some calculations of the user profile and save it in the database. But the problem with the method in the user model:
UserSchema.pre('save', function(next) {
if (this.password && this.password.length > 6) {
this.salt = new Buffer(crypto.randomBytes(16).toString('base64'), 'base64');
this.password = this.hashPassword(this.password);
}
next();
});
If I send the password with my changes, it will change the credentials, so the user will not be able to log in next time. I want to remove the password from the user object before saving, but I can not do this (let's look at the comments in my code below):
exports.signin = function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
if (err || !user) {
res.status(400).send(info);
} else {
req.login(user, function(err) {
if(err) {
res.status(400).send(err);
} else {
console.log(delete user.password);
console.log(user.password);
}
});
}
})(req, res, next);
};
What happened? Why does the delete method return true, but nothing happens? Thanks for the help:)
source
share