node js passport js更改用户的密码

时间:2017-06-19 15:16:48

标签: node.js passport.js

我正在使用节点js创建应用程序。在这个应用程序中,我已经通过护照js完成了用户登录和注册。所以现在我需要提供对已登录用户的访问权限以更改密码。因此,我尝试以自己的方式执行此操作,但是当我运行此过程时,更改的密码不会更新并将其保存到已登录用户的mongoose文档中。我将提供用于该过程的代码。所以我请你们,请让我知道我怎么能用我的程序做到这一点。

这是我更改密码的POST路由。

app.post('/changePass/:hash', isLoggedIn, function(req, res){
    cph.findOne({hash: req.params.hash}).populate('userId', "local.password -_id").exec(function(err, hash){
        if(err) throw err;
        if(validator.isEmpty(req.body.currentPassword) || validator.isEmpty(req.body.newPassword) || validator.isEmpty(req.body.confirmPassword)){
            res.render('admin/settings/pages/cup/cpf', {
                user: req.user,
                message: 'Fields must be required',
                data: hash
            });
        }
        else {
            if(!bcrypt.compareSync(req.body.currentPassword, hash.userId.local.password)){
                res.render('admin/settings/pages/cup/cpf', {
                    user: req.user,
                    message: 'Current password is incurrect',
                    data: hash
                });
            }
            else {
                if(req.body.newPassword != req.body.confirmPassword){
                    res.render('admin/settings/pages/cup/cpf', {
                        user: req.user,
                        message: 'New password and confirm password do not match',
                        data: hash
                    });
                }
                else {
                    cph.update({$set:{'userId.local.password': bcrypt.hashSync(req.body.confirmPassword, bcrypt.genSaltSync(8), null)}}, function(){
                        console.log('Success')
                    });
                }
            }
        }
    });
});

这是mongoose集合,它创建一个哈希来更改密码发送,作为记录用户电子邮件的组合链接。

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var bcrypt = require('bcrypt-nodejs');

var cpHashSchema = Schema({
    userId: {
        type: Schema.ObjectId,
        ref: 'users'
    },
    hash: {
        type: String
    }
});

module.exports = mongoose.model('changepasswordHash', cpHashSchema);

这是用户的收藏

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var bcrypt = require('bcrypt-nodejs');

var userSchema = Schema({
    active: {
        type: Boolean,
        default: false
    },
    first: {
        type: String
    },
    last: {
        type: String
    },
    email: {
        type: String
    },
    local: {
        username: {
            type: String
        },
        password: {
            type: String
        }
    },
    joined: {
        type: Date,
        default: Date.now
    },
    usertype: {
        type: String,
        default: 'user'
    }
});

userSchema.methods.generateHash = function(password) {
    return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
};

userSchema.methods.validPassword = function(password) {
    return bcrypt.compareSync(password, this.local.password);
};

module.exports = mongoose.model('users', userSchema);

这些是我用来构建此应用程序的源代码。所以大家请帮我完成这个应用程序。

谢谢

1 个答案:

答案 0 :(得分:1)

首先 - 您尝试使用另一个表中的字段更新changepasswordHash集合。 MongoDB无法更新相关记录。

您必须使用userId更新users集合:

users.update({_id: hash.userId._id}, {$set: {'local.password': newPass}}, callbackHere)
相关问题