首页 > 解决方案 > 如何在重置密码时使用新密码更新密码

问题描述

我正在使用代码存储库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);

标签: node.jsmongodbexpressmongoosenext.js

解决方案


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

推荐阅读