【问题标题】:node js passport js change user's password节点js护照js更改用户密码
【发布时间】:2017-06-20 00:21:11
【问题描述】:

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

这是我更改密码的 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);

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

谢谢

【问题讨论】:

    标签: node.js passport.js


    【解决方案1】:

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

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

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

    【讨论】:

    • 在这个 users.update({_id: hash.userId._id} object' 中应该做什么。请多解释一下
    • Mongoose 将您的用户作为第二个查询加载到用户集合。然后将其添加到哈希对象中。因此,您将用户集合中的所有字段都放入 hash.userId 属性中。包含您的_id 和其他道具。
    猜你喜欢
    • 2017-08-18
    • 1970-01-01
    • 2020-02-13
    • 2021-07-25
    • 2023-03-22
    • 2017-05-12
    • 1970-01-01
    • 1970-01-01
    • 2014-11-22
    相关资源
    最近更新 更多