Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix field name of reset password token, add verifier and set status #161

Closed
wants to merge 8 commits into from
19 changes: 12 additions & 7 deletions api/controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ async function forgotPassword(req, res) {
const response = {
success: 1,
userid: user.id,
tomivm marked this conversation as resolved.
Show resolved Hide resolved
url: token,
//url: token,
tomivm marked this conversation as resolved.
Show resolved Hide resolved
message: 'Success! Check your mail to reset your password.'
};
return res.status(200).json(response);
Expand All @@ -436,7 +436,6 @@ async function forgotPassword(req, res) {
}
async function storePassword(req, res) {
const { userid, password, token } = req.body;

try {
const resetPassword = await ResetPassword.findOne({
userId: userid,
Expand All @@ -448,10 +447,16 @@ async function storePassword(req, res) {
error: err.message
});
}
// the token and the hashed token in the db are verified befor updating the password
bcrypt.compare(token, resetPassword.token, function(errBcrypt, resBcrypt) {
let expireTime = moment.utc(resetPassword.expire);
// the token and the hashed token in the db are verified before updating the password
bcrypt.compare(token, resetPassword.resetPasswordToken, function(errBcrypt, resBcrypt) {
let expireTime = moment.utc(resetPassword.expire); // expireTime and currentTime is never used
tomivm marked this conversation as resolved.
Show resolved Hide resolved
let currentTime = new Date();
if(!resBcrypt){
tomivm marked this conversation as resolved.
Show resolved Hide resolved
return res.status(500).json({
message: 'Error resetting user password.',
error: 'invalid Token'
});
}
//hashing the password to store in the db node.js
bcrypt.genSalt(8, function(err, salt) {
bcrypt.hash(password, salt, async function(err, hash) {
Expand All @@ -464,8 +469,8 @@ async function storePassword(req, res) {
message: 'No user found with that ID.'
});
}
ResetPassword.findOneAndUpdate(
{ id: resetPassword.id },
ResetPassword.findByIdAndUpdate(
resetPassword._id,
{ status: true },
function(err) {
if (err) {
Expand Down