【问题标题】:How to update Password with new password on Reset Password如何在重置密码时使用新密码更新密码
【发布时间】:2020-10-25 15:24:59
【问题描述】:

我正在使用的 GITHUB REPO code repository

我正在尝试在将用户重定向到重置密码页面时重置用户密码。在第一次 Singup 中,我使用 CRYPTO 对密码进行哈希处理,然后生成 salt 并将其存储在数据库中。在重置密码时,新密码没有得到更新,它不允许使用更新的密码登录。 我尝试使用提供更新密码的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 毫秒内过期
  • 我试过 { expiresIn : '2d' } ,仍然给我同样的错误。 updated github repo
  • 问题可能与重置密码控制器方法有关,在注册用户时,您正在对密码进行哈希处理,但对于重置密码,您并没有这样做,请尝试对重置密码进行哈希处理。
  • @Nitin 不需要散列重置密码,因为用户模型在“密码”虚拟属性上包含设置器。当从 resetPassword 控制器方法调用 user.save() 时,它将调用密码设置器并存储散列新密码的密码
  • @jarivak 我已经从问题第一行中提到的 Git 存储库中获取了后端代码文件夹,并且只做了我在之前的评论 {expiresIn:'1d'} 中提到的一个更改。在那之后我运行我的代码并使用邮递员检查后端代码,所有功能都正常工作,新密码正确更新,并允许我们使用新密码登录,因此无需在后端更改任何内容。所以,我建议你不要只关注在后端调试您的前端代码并确保将正确的 resetPasswordLink 从前端传递给 resetpassword 控制器

标签: node.js mongodb express mongoose next.js


【解决方案1】:

问题出在您的登录功能中,您设置了 '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' });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    • 1970-01-01
    • 2020-12-20
    • 2014-12-03
    • 2013-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多