Skip to content

Commit

Permalink
Implemented resetPassword route for resetting the password of user in…
Browse files Browse the repository at this point in the history
… database
  • Loading branch information
devanshkansagra committed May 8, 2024
1 parent b5bf5fd commit 2294625
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ app.post('/forgot', async (req, res) => {
res.cookie('OTP', code, {
maxAge: 60000,
path: "/"
}).cookie('Email', email, {
maxAge: 60000,
path: "/"
})
.status(200).send({ success: "User Found" });
}
Expand All @@ -185,6 +188,30 @@ app.post('/forgot', async (req, res) => {
}
})

app.post('/resetPassword', async (req, res) => {
if(req.headers.cookie) {
const cookieEmail = req.cookies.Email;
const cookieOTP = req.cookies.OTP;
const {password, otp} = req.body.data;

try {
const updatePassword = await user.updateOne({email: cookieEmail}, {password: password});
if(updatePassword && (otp === cookieOTP)) {
res.status(200).send({message: "Password Updated Successfully"});
}
else if(otp !== cookieOTP) {
res.status(401).send({message: "Unauthorized"});
}
}
catch(error) {
res.status(500).send({message: "Server Error"});
}
}
else {
console.log("Cookies weren't set by client side");
}
})

app.listen(port, () => {
console.log('Server is started at port ', port);
})

0 comments on commit 2294625

Please sign in to comment.