0
我正在使用节点js创建应用程序。在这个应用程序中,我已经通过护照js完成了用户登录和注册。所以现在我需要提供访问记录的用户来更改密码。所以我试图以我自己的方式做到这一点,但是当我运行此过程时,更改后的密码不会更新并将其保存到登录用户的猫鼬文档中。我将提供我用于该过程的代码。所以我请求你们,请让我知道我如何在我的程序中做到这一点。节点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')
});
}
}
}
});
});
这是猫鼬收集,创建一个哈希更改密码发送作为组合的链接登录用户的电子邮件。
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);
这些都是我用来构建这个应用程序的源代码。所以,请大家帮我完成这个应用程序。
谢谢
({_ ID:hash.userId._id}对象”它应该做的,请解释一下多一点 –
它的工作非常感谢 –
Mongoose将你的用户作为第二个查询加载到用户集合中,然后将其添加到哈希对象中,这样你就可以将用户集合中的所有字段都包含到'hash.userId'属性中,包含'_id'和其他道具。 –