Friday 5 May 2017

Changing Password in Node.js

I'm using Node.js, Express and MongoDB. I'm creating a password reset feature.

I've found this solution: it checks whether your username is correct or not, if yes, then it changes the password, otherwise it shows an error.

It works perfectly with username, but I want to use email rather than username to verify the user.

This is what im doing:

app.post("/reset", function (req, res) {
    User.findByUsername(req.body.username).then(function (sanitizedUser) {
        if (sanitizedUser) {
            sanitizedUser.setPassword(req.body.password, function () {
                sanitizedUser.save();
                req.flash("success", "password resetted");
                res.redirect("/login");
            });
        } else {
            req.flash("error", "User doesnt exist");
            res.redirect("/reset");
        }
    }, function (err) {
        console.log(err);
        res.redirect("/");
    });
});

This is what I want:

    User.find({email:req.body.email}).then(function (sanitizedUser) {
        // ...snip...
    });

But when I use it to find using email, the page just keeps reloading, without showing any error (if wrong) or change the password (if correct).

What seems to be the problem?



via Rohit Hooda

No comments:

Post a Comment