implement the actual resetting endpoint

This commit is contained in:
Arjun Patel
2019-05-13 01:16:56 -07:00
parent c4dd7cd1fa
commit c657b2b9c5
5 changed files with 163 additions and 96 deletions
+71 -6
View File
@@ -100,7 +100,7 @@ exports.resetPasswordEmail = async (req, res, next) => {
user = await sequelize.query( user = await sequelize.query(
` `
SELECT id FROM organizations SELECT id FROM organizations
WHERE email = :email`, WHERE primary_contact_email = :email`,
{ {
type: sequelize.QueryTypes.SELECT, type: sequelize.QueryTypes.SELECT,
replacements: { email } replacements: { email }
@@ -109,10 +109,7 @@ exports.resetPasswordEmail = async (req, res, next) => {
} }
user = user[0]; user = user[0];
if (!user) if (!user) return next(errorMaker(400, 'Not a valid user'));
return res.status(400).json({
message: 'User not found'
});
const CODE = uuidv4(); const CODE = uuidv4();
const currDate = new Date(); const currDate = new Date();
@@ -146,7 +143,8 @@ exports.resetPasswordEmail = async (req, res, next) => {
} }
); );
sendgrid.passwordResetEmail(email, CODE); await sendgrid.passwordResetEmail(email, CODE);
return res return res
.status(200) .status(200)
.json({ message: 'Successfuly sent password reset email!' }); .json({ message: 'Successfuly sent password reset email!' });
@@ -154,3 +152,70 @@ exports.resetPasswordEmail = async (req, res, next) => {
next(e); next(e);
} }
}; };
exports.resetPassword = async (req, res, next) => {
try {
const { password, code } = req.query;
var passwordResetRow = await sequelize.query(
`
SELECT * FROM password_reset
WHERE code = :code`,
{
type: sequelize.QueryTypes.SELECT,
replacements: { code }
}
);
if (!passwordResetRow.length)
return next(errorMaker(400, 'Forbidden to reset password'));
passwordResetRow = passwordResetRow[0];
//TODO check the timestamp of the code creation
const createdDate = new Date(passwordResetRow.created_at);
const currDate = new Date();
const diffTime = Math.abs(currDate.getTime() - createdDate.getTime());
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
if (diffDays > 3) return next(errorMaker(400, 'Code expired'));
const hashedPass = bcrypt.hashSync(password);
if (passwordResetRow.user_type == 'donor') {
await sequelize.query(
`
UPDATE donors
SET password = :password
WHERE id = :user_id
`,
{
type: sequelize.QueryTypes.UPDATE,
replacements: {
password: hashedPass,
user_id: passwordResetRow.user_id
}
}
);
} else {
await sequelize.query(
`
UPDATE organizations
SET password = :password
WHERE id = :user_id
`,
{
type: sequelize.QueryTypes.UPDATE,
replacements: {
password: hashedPass,
user_id: passwordResetRow.user_id
}
}
);
}
return res.status(200).json({ message: 'Successfuly reset password!' });
} catch (e) {
next(e);
}
};
+5 -3
View File
@@ -86,17 +86,19 @@ exports.passwordResetEmail = async (receiver, code) => {
const msg = { const msg = {
to: receiver, to: receiver,
from: { email: '[email protected]', name: 'UCharify Security' }, from: { email: '[email protected]', name: 'UCharify Security' },
template_id: 'd-569f804f5e7749cba66bac1994607280', template_id: 'd-f26ce08089914f3a983f8932913ed262',
substitutionWrappers: ['{{', '}}'], substitutionWrappers: ['{{', '}}'],
dynamic_template_data: { dynamic_template_data: {
reset_link: `${process.env.WEB_CLIENT}/resetpassword/form`, reset_link: `${
process.env.WEB_CLIENT
}/resetpassword/form?code=${code}`,
subject: 'Reset Your Password' subject: 'Reset Your Password'
} }
}; };
const sentRes = await sgMail.send(msg); const sentRes = await sgMail.send(msg);
resolve(); resolve(sentRes);
} catch (e) { } catch (e) {
reject(e); reject(e);
} }
+1 -1
View File
@@ -14,6 +14,6 @@ router.post('/org/login', auth.orgLogin);
router.get('/resetpassword', auth.resetPasswordEmail); router.get('/resetpassword', auth.resetPasswordEmail);
// Reset password for either donor or charity after verifying code // Reset password for either donor or charity after verifying code
router.post('/resetpassword', auth.resetPasswordEmail); router.post('/resetpassword', auth.resetPassword);
module.exports = router; module.exports = router;
+5 -5
View File
@@ -4,24 +4,24 @@ module.exports = (sequelize, DataTypes) => {
'password_reset', 'password_reset',
{ {
code: { code: {
type: Sequelize.STRING, type: DataTypes.STRING,
allowNull: false allowNull: false
}, },
user_id: { user_id: {
type: Sequelize.UUID, type: DataTypes.UUID,
allowNull: false allowNull: false
}, },
user_type: { user_type: {
type: Sequelize.STRING, type: DataTypes.STRING,
allowNull: false allowNull: false
}, },
created_at: { created_at: {
allowNull: false, allowNull: false,
type: Sequelize.DATE type: DataTypes.DATE
}, },
updated_at: { updated_at: {
allowNull: false, allowNull: false,
type: Sequelize.DATE type: DataTypes.DATE
} }
}, },
{} {}