Open icecreamsandwich opened 6 years ago
I'm having the issue here, please let me know if you solve this, and I will let you know too, if I come up with a solution. Thanks
Hi, I figure it out
Here is what i did in my controller handling the reset password ,
exports.editPassword = async (req, res) => { const user = await User.findOne({ username: req.user.username }); await user.setPassword(req.body.password); const updatedUser = await user.save(); req.login(updatedUser); req.flash('success', 'Password Changed Successfully') res.redirect('back') }
from the Documentation Passport-local-mongoose ,
you first need to get the specific user to update the password , here in my case the current login user , which is available on the req.user which we are exposed to , you can use any of the return property to query your collection, using async await i made a variable to hold the return object, in my case 'user', thereafter i chained the setProperty on it passing in the new password(req.body.password) since it return a promise i await it and assign a variable to it. from here you are good ...
Note: since it is a promise it either resolved or reject, handling error can be done by rapping your code in a safe blanket, try..catch
. You can read more Here
OK , i resolved the problem . I did the same way . earlier the User model was not available to reset the password . I inherited user from the model and checked the user is logged in and then used it
User.findById(userid).then(function(sanitizedUser){
if (sanitizedUser){
sanitizedUser.setPassword(newpassword, function(){
sanitizedUser.save();
// res.status(200).json({message: 'password reset successful'});
res.redirect("/reset-password");
});
} else {
res.status(500).json({message: 'This user does not exist'});
}
},function(err){
console.error(err);
})
Alright we are all good then..
Since changePassword
is a schema method, it must be used on an instance of a model, not the model itself or the imported passportLocalMongoose.
UserModel.findById(req.user._id)
.then(foundUser => {
foundUser.changePassword(req.body.old, req.body.new)
.then(() => {
console.log('password changed');
})
.catch((error) => {
console.log(error);
})
})
.catch((error) => {
console.log(error);
});
I know this is an old issue, but seeing as the last reply was 6 months ago - I thought I'd update it with a little syntax sugar..
try {
//your validation/sainitization
const { currentPass, newPass } = req.body;
const { user, error } = await User.authenticate()(
req.user.email,
currentPass
);
if (!user || error) {
throw error;
}
await user.changePassword(currentPass, newPass);
//token/session invalidation & redirect to login on front-end
res.sendStatus(200);
} catch (error) {
//your error handling
}
I'm reauthing the user because I'm using JWTs, but as mentioned above you could do something like the following:
const user = await UserModel.findById(req.user._id)
user.changePassword('oldpass','newPass')
I am using passport and passport-local-mongoose for authentication in my react app . The login and registration works without any
problem . But the reset password / change password is not working properly . I have the reset password component as follows ResetPassword.js
and in server.js file
When submitting it throws me error :
What is the reason ? how to solve it ?
package.json