Working on password recovery

This commit is contained in:
2021-07-28 23:02:04 +10:00
parent 72b3babfd8
commit 81da8ca422
9 changed files with 224 additions and 1 deletions
+59
View File
@@ -0,0 +1,59 @@
//libraries
const bcrypt = require('bcryptjs');
const { accounts, recovery } = require('../database/models');
//auth/reset
const route = async (req, res) => {
//validate the given details
const validateErr = await validateDetails(req.query, req.body);
if (validateErr) {
return res.status(401).send(validateErr);
}
//generate the password hash
const hash = await bcrypt.hash(req.body.password, await bcrypt.genSalt(11));
//update the account data
accounts.update({
hash: hash
}, {
where: {
email: req.query.email
}
})
//delete from the recovery table
recovery.destroy({
where: {
email: req.query.email
}
});
return null;
};
const validateDetails = async (query, body) => {
//verify the recovery record exists
const record = recovery.findOne({
email: query.email,
token: query.token
});
if (!record) {
return 'Failed to recover a password';
}
//validate password
if (!body.password) {
return 'Missing password';
}
if (body.password.length < 8) {
return 'Password too short';
}
return null;
};
module.exports = route;