Home > front end >  Password reset not saving to database
Password reset not saving to database

Time:06-01

My code above runs without any errors but the new password isn't saved.

I've been following the bcrypt docs, a blog post and a video and think the three different sources have resulted in my missing something critical. Any ideas why the new password isn't being saved?

module.exports.submitNewPassword = async (req, res) => {
    const slidedHeaderToken = req.headers.referer.slice(-40);
    const artist = await Artist.find({ resetPasswordToken: slidedHeaderToken, resetPasswordExpires: { $gt: Date.now() } });
    if (!artist) {
        console.log("Artist doesn't exist");
    } else {
        const hashedPassword = async (pw) => {
            bcrypt.hash(req.body.password, 12)
        }
        hashedPassword()
        artist.password = hashedPassword;
        resetPasswordToken = null;
        resetPasswordExpires = null;

        console.log("Successfully resubmitted password");
        res.redirect('login');
    }
}

CodePudding user response:

You should call await artist.save() function after you set some fields

CodePudding user response:

When you make a change to the data using Find, you need to save it.

await yourModel.save()

CodePudding user response:

artists is an array of all the artists matching the parameters in the call to .find(). If you just want to find one, use .findOne().

Then after you modify it, use .save() to save it back to the database.

module.exports.submitNewPassword = async (req, res) => {
    const slidedHeaderToken = req.headers.referer.slice(-40);
    const artist = await Artist.findOne({ resetPasswordToken: slidedHeaderToken, resetPasswordExpires: { $gt: Date.now() } });
    if (!artist) {
        console.log("Artist doesn't exist");
    } else {
        const hashedPassword = async (pw) => {
            bcrypt.hash(req.body.password, 12)
        }
        hashedPassword()
        artist.password = hashedPassword;
        await artist.save();
        resetPasswordToken = null;
        resetPasswordExpires = null;
        console.log("Successfully resubmitted password");
        res.redirect('login');
    }
}

See the Mongoose tutorial here.

  • Related