kingbeyondthewall1
kingbeyondthewall1

Reputation: 43

how to change password using passport-local-mongoose?

I have imported the passport-local-mongoose to my code and I tried to register and log in users and all went successfuly but now I want to change the password of a specific user, how can I do that? I've read the documentation of the passport-local-mongoose and it says you can change your password by using this method

changePassword(oldPassword, newPassword, [cb])

but I don't know how to add that cb field how can I do that?

Upvotes: 4

Views: 183

Answers (1)

Dastan
Dastan

Reputation: 2106

it will be something like this:

app.post('/changePassword', function (req, res) {
    if (typeof req.user === 'undefined') {
        res.redirect('/login')
    } else {
        User.findOne({ _id: req.user._id }, function (err, user) {
            if (!err) {
                user.changePassword(req.body.oldPassword, req.body.newPassword, function (err) {
                    if (!err) {
                        res.redirect('/login')
                    } else {
                        console.log(err);
                    }
                })
            } else {
                console.log(err);
            }
        })
    }
})

Upvotes: 3

Related Questions