Node.js 如何在重置密码时使用新密码更新密码
我正在使用的GITHUB回购协议 我试图在重定向用户重置密码页面时重置用户密码。在第一次搜索中,我对密码进行哈希运算,然后使用生成salt并存储在数据库中。重置密码时,新密码未更新,因此不允许使用更新的密码登录。 我尝试使用Node.js 如何在重置密码时使用新密码更新密码,node.js,mongodb,express,mongoose,next.js,Node.js,Mongodb,Express,Mongoose,Next.js,我正在使用的GITHUB回购协议 我试图在重定向用户重置密码页面时重置用户密码。在第一次搜索中,我对密码进行哈希运算,然后使用生成salt并存储在数据库中。重置密码时,新密码未更新,因此不允许使用更新的密码登录。 我尝试使用response.password提供更新的密码。仍然无法找到解决方案 重置密码: exports.resetPassword = (req,res) => { const {resetPasswordLink, newPassword } = req.b
response.password
提供更新的密码。仍然无法找到解决方案
重置密码:
exports.resetPassword = (req,res) => {
const {resetPasswordLink, newPassword } = req.body
if(resetPasswordLink){
jwt.verify(resetPasswordLink,process.env.JWT_RESET_PASSWORD, function(err,decoded){
if(err){
return res.status(401).json({
error : ' The Link has been expired ! , Try Again '
})
}
User.findOne({resetPasswordLink},(err,user)=>{
if(err || !user){
return res.status(401).json({
error: ' The Link has been expired ! , Try Again '
})
}
const updatedFields = {
password: newPassword,
resetPasswordLink: ''
}
user = _.extend(user,updatedFields)
user.save((err,result)=>{
if(err){
return res.status(400).json({
error: errorHandler(err)
})
}
return res.json({
message: ` Your Password Has Been Successfully Reset , Please Return to the SignIn Page to SignIn `
// result.password
})
})
})
})
}
}
8月4日更新:
以下是完整的用户模型
用户架构:
const mongoose = require('mongoose');
const crypto = require('crypto');
const userSchema = new mongoose.Schema(
{
username: {
type: String,
trim: true,
required: true,
max: 32,
unique: true,
index: true,
lowercase: true
},
name: {
type: String,
trim: true,
required: true,
max: 32
},
email: {
type: String,
trim: true,
required: true,
unique: true,
lowercase: true
},
profile: {
type: String,
required: true
},
hashed_password: {
type: String,
required: true
},
salt: String,
about: {
type: String
},
role: {
type: Number,
default: 0
},
photo: {
data: Buffer,
contentType: String
},
resetPasswordLink: {
data: String,
default: ''
}
},
{ timestamp: true }
);
userSchema
.virtual('password')
.set(function(password) {
// create a temporarity variable called _password
this._password = password;
// generate salt
this.salt = this.makeSalt();
// encryptPassword
this.hashed_password = this.encryptPassword(password);
})
.get(function() {
return this._password;
});
userSchema.methods = {
authenticate: function(plainText) {
return this.encryptPassword(plainText) === this.hashed_password;
},
encryptPassword: function(password) {
if (!password) return '';
try {
return crypto
.createHmac('sha1', this.salt)
.update(password)
.digest('hex');
} catch (err) {
return '';
}
},
makeSalt: function() {
return Math.round(new Date().valueOf() * Math.random()) + '';
}
};
module.exports = mongoose.model('User', userSchema);
问题是在您的登录函数中,您设置了'jwt'和'cookie'的过期时间,使用{expiresIn:'1d'}而不是{expiresIn:'1'},因为'1'表示您的jwt和cookie在1ms内过期
const token = jwt.sign({ _id: user._id }, process.env.JWT_SECRET, { expiresIn: '1d' });
res.cookie('token', token, { expiresIn: '1d' });
可能问题在于您的登录函数中设置了'jwt'和'cookie'的过期时间,使用了{expiresIn:'1d'}而不是{expiresIn:'1'},因为'1'意味着您的jwt和cookie在1 msI中过期,使用{expiresIn:'2d'}尝试过,但仍然给我相同的错误。问题可能出在重置密码控制器方法上,在注册用户时,您正在对密码进行哈希运算,但要重置密码,您没有进行相同的操作,请尝试对重置密码进行哈希运算。@Nitin不需要对重置密码进行哈希运算,因为用户模型包含“password”虚拟属性上的setter。when user.save()从resetPassword控制器方法调用,它将调用密码设置器并存储新密码的哈希密码password@jarivak我已经从问题的第一行提到的Git repo中获取了后端代码文件夹,并且只做了我在前面的评论{expiresIn:'1d}中提到的一个更改。之后,我运行我的代码并使用postman检查后端代码,所有功能正常运行,新密码正确更新,允许我们使用新密码登录,因此不需要在后端更改任何内容。因此,我建议您不要只关注后端,而是调试前端代码,并确保从前端将正确的resetPasswordLink传递给resetpassword控制器