I have an issue when user chooses forgot password. The forgot password form appears user enters email, user get’s alert to check there email and user clicks link which take you to reset form. The user type in new password password. I get alert saying password reset successful. User gets routed to login page. When I type in new password and it says Password or User name is incorrect. So its not being updated. I’m thinking it has something to do with way I’m doing my update. It not being updated in database. My console.log
and alerts confirm its reaching service and endpoints. If someone can please point me in the right direction I would be so grateful. . I have included only necessary code snippet:
Reset Password Component:
let resetObj={
token: this.token,
Password: this.form.value.password
}
//This gets called on submit
this.accountService.resetPassword(resetObj)
.subscribe({
next: () => {
this.alertService.success('Password reset successful, you can now login',
{ keepAfterRouteChange: true });
this.form.reset();
this.router.navigate(['../../login'], { relativeTo: this.route });
},
error: error => {
this.alertService.error(error);
}
});
accountService:
resetPassword(resetObj: any) {
return this.http.post(`${this.apiUrl}/reset-password`, resetObj);
}
endpoint:
controller.js
router.post('/reset-password', passwordReset)
function passwordReset(req,res, next){
console.log("Does it goet to password reset");
const token = req.body.token;
const newPassword = req.body.password;
console.log("What do properties hold" + " " + " " + newPassword + " " + " " +
token)
userService.resetPassword(token, newPassword)
.then(() =>res.json({}))
.catch(err => next(err));
}
userservice:
async function resetPassword(token, newPassword, result,res){
console.log("InsideresetPassword Service")
console.log("whats in token", token);
console.log("What is new password", newPassword);
jwt.verify(token,process.env.JWT_Secret, async(err, data) =>{
console.log("Does it get to this if")
if(err){
console.log("Reset Link is Expired");
}else{
console.log("Does it get to else in reset service")
const response = data;
const user = await User.findOne({email: {$regex: '^'+ response.email+"$",
$options: 'i'}});
console.log("Whats email", user.email)
const salt = await bcrypt.genSalt(10);
const encryptedPassword = await bcrypt.hash(newPassword, salt);
user.password = encryptedPassword;
try{
const updateUser = await User.findOneAndUpdate(
{_id: user._id},
{$set: user},
{new: true} //can show update
)
console.log( "Password Updated")
}catch(error){
console.log("Something went wrong while redirecting passwor
}
}
})
}