node.js - 如何在重置密码时使用新密码更新密码
问题描述
我正在使用代码存储库的GITHUB REPO
我正在尝试在将用户重定向到重置密码页面时重置用户密码。在第一次 Singup 中,我正在对密码进行哈希处理,并使用CRYPTO生成并存储在数据库中的盐。在重置密码时,新密码没有得到更新,它不允许使用更新的密码登录。我尝试使用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 日更新:这是完整的 USER 模型
用户架构:
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 在 1 毫秒内到期
const token = jwt.sign({ _id: user._id }, process.env.JWT_SECRET, { expiresIn: '1d' });
res.cookie('token', token, { expiresIn: '1d' });
推荐阅读
- reactjs - 无法在打字稿反应js中映射对象
- python - “删除”删除所有内容
- html - 如何解决 Bootstrap 4 中的响应问题
- android - 使用 firebase 数据库持久性和 firebase 匿名身份验证时是否必须重新进行身份验证?
- c# - 在 Unity 中的两种情况下都销毁游戏对象
- java - 从 JAR 启动时,ServiceLoader 的工作方式不同
- keras - 为什么 one_hot 为 2 个不同的单词提供相同的值?
- makefile - 如何在 linux 中使用 ifort 编译自由格式和固定格式的 Fortran 代码
- c# - 应用启动时创建 C# MVC 本地数据库
- excel - 在vba代码错误中打开带有url的excel文件