Bcrypt不保存密码?

时间:2017-03-09 17:43:14

标签: node.js mongodb email express passport.js

出于某种原因,当我尝试哈希并保存密码时,新密码不会保存到数据库中?我正在使用MongoDB,NodeJS和护照让用户更改密码。

UserSchema.pre('save', function(next) {
  var user = this;
  var SALT_FACTOR = 5;
console.log('trying to save the password')

  if (!user.isModified('password')) return next();

  bcrypt.genSalt(SALT_FACTOR, 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();
});
});
});

发布保存:

app.post('/reset/:token', function(req, res) {
  async.waterfall([
    function(done) {
      User.findOne({ resetPasswordToken: req.params.token, resetPasswordExpires: { $gt: Date.now() } }, function(err, user, next) {
        if (!user) {
          req.flash('error', 'Password reset token is invalid or has expired.');
          return res.redirect('back');
        }


        user.password = req.body.password;
        user.resetPasswordToken = undefined;
        user.resetPasswordExpires = undefined;
        console.log('password' + user.password  + 'and the user is' + user)

  user.save(function(err) {
  if (err) {
      console.log('here')
       return res.redirect('back');
  } else { 
      console.log('here2')
    req.logIn(user, function(err) {
      done(err, user);
    });

  }
        });
      });
    },

1 个答案:

答案 0 :(得分:0)

稍微重组一下。将用户更新功能移到模型中,这样你就有了一个像这样的user.js文件(例如models / user.js):

var mongoose = require('mongoose');
var bcrypt = require('bcrypt');
var db = mongoose.connection;

var UserSchema = mongoose.Schema({
    email: {
        type: String
    },
    password: {
        type: String,
        required: true,
        bcrypt: true
    }
    // more fields etc
});

var User = module.exports = mongoose.model('User', UserSchema);

module.exports.updateUser = function(newUser, userId, callback) {
    if (newUser.password != "" && newUser.password != undefined) {
        bcrypt.hash(newUser.password,10,function(err,hash){
            if (err) throw err;
            newUser.password = hash;
            var upsertData = newUser.toObject();
            delete upsertData._id;
            User.update({_id: userId}, upsertData, {upsert:true}, callback);
        });
    } else {
        var upsertData = newUser.toObject();
        delete upsertData._id;
        delete upsertData.password;
        User.update({_id: userId}, upsertData, {upsert:true}, callback);
    };
}

然后在您的路线/应用文件中,您可以添加我猜您正在做的用户模型(例如routes / user.js):

var User = require('../models/user');

并像这样使用它:

var newUser = new User({
    password: password,
    field: value,
    field: value,
    field: value etc
});
User.updateUser(newUser, userId, function(){
    // rest of your code
});

通过这种方式,您可以使用可重复使用的功能,无论您传递给它的字段数是多少,都会更新用户。

顺便说一下,你的bcrypt使用和我的实际上是相同的。即。

bcrypt.hash(myPlaintextPassword, saltRounds, function(err, hash) {

与:

相同
bcrypt.genSalt(saltRounds, function(err, salt) {
    bcrypt.hash(myPlaintextPassword, salt, function(err, hash) {

来自他们的文档:https://www.npmjs.com/package/bcrypt