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'和其他道具。 –