Skip to content

Commit

Permalink
fix(helpers): fix mails error catch
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreBrisorgueil committed Mar 5, 2020
1 parent a0ef405 commit 07ebd21
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 41 deletions.
21 changes: 11 additions & 10 deletions lib/helpers/mail.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const nodemailer = require('nodemailer');

const config = require(path.resolve('./config'));
const AppError = require(path.resolve('./lib/helpers/AppError'));
const responses = require(path.resolve('./lib/helpers/responses'));
const smtpTransport = nodemailer.createTransport(config.mailer.options);

/**
Expand All @@ -19,18 +20,18 @@ const smtpTransport = nodemailer.createTransport(config.mailer.options);
* @param {String} subject
* @return {String} true
*/
exports.sendMail = (res, template, params, from, to, subject) => {
res.render(template, params, (err, html) => {
exports.sendMail = async (res, mail, answer) => {
await res.render(mail.template, mail.params, async (err, html) => {
if (!err) {
smtpTransport.sendMail({
from,
to,
subject,
const send = await smtpTransport.sendMail({
from: mail.from,
to: mail.to,
subject: mail.subject,
html,
}, (err) => {
if (!err) return { type: 'success' };
return new AppError(err, { code: 'HELPER_ERROR' });
});
}).then(() => 'success')
.catch((error) => error);
if (send !== 'success') return responses.error(res, 400, 'Bad Request', answer.error)(answer.data);
responses.success(res, answer.success)(answer.data);
}
return new AppError(err, { code: 'HELPER_ERROR' });
});
Expand Down
64 changes: 33 additions & 31 deletions modules/users/controllers/users/users.password.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,23 @@ exports.forgot = async (req, res) => {
}

// send mail
mail.sendMail(
res, 'reset-password-email', {
displayName: user.displayName,
url: `${config.cors.protocol}://${config.cors.host}:${config.cors.port}/auth/password-reset?token=${user.resetPasswordToken}`,
appName: config.app.title,
appContact: config.app.appContact,
},
config.mailer.from,
user.email,
'Password Reset',
);
responses.success(res, 'An email has been sent to the provided email with further instructions.')();
mail.sendMail(res,
{
template: 'reset-password-email',
from: config.mailer.from,
to: user.email,
subject: 'Password Reset',
params: {
displayName: user.displayName,
url: `${config.cors.protocol}://${config.cors.host}:${config.cors.port}/auth/password-reset?token=${user.resetPasswordToken}`,
appName: config.app.title,
appContact: config.app.appContact,
},
}, {
success: 'An email has been sent to the provided email with further instructions.',
error: 'Failure sending email',
data: null,
});
};

/**
Expand Down Expand Up @@ -79,11 +84,8 @@ exports.validateResetToken = async (req, res) => {
*/
exports.reset = async (req, res) => {
let user;
let send;

// check input
if (!req.body.token || !req.body.newPassword) return responses.error(res, 400, 'Bad Request', 'Password or Token fields must not be blank')();

// get user by token, update with new password, login again
try {
user = await UserService.search({
Expand All @@ -109,23 +111,23 @@ exports.reset = async (req, res) => {
} catch (err) {
responses.error(res, 422, 'Unprocessable Entity', errors.getMessage(err))(err);
}

// send mail
mail.sendMail(
res, 'reset-password-confirm-email', {
displayName: user.displayName,
appName: config.app.title,
appContact: config.app.appContact,
},
config.mailer.from,
user.email,
'Your password has been changed',
);

const template = mail.generateTemplate(res, 'reset-password-confirm-email', user.displayName);
if (template) send = mail.sendMail(config.mailer.from, user.email, 'Your password has been changed', template);
if (!template || !send) return responses.error(res, 400, 'Bad Request', 'Failure sending email')();
responses.success(res, 'An email has been sent to the provided email with further instructions.')();
mail.sendMail(res,
{
template: 'reset-password-confirm-email',
from: config.mailer.from,
to: user.email,
subject: 'Your password has been changed',
params: {
displayName: user.displayName,
appName: config.app.title,
appContact: config.app.appContact,
},
}, {
success: 'An email has been sent to the provided email with further instructions.',
error: 'Failure sending email',
data: null,
});
};

/**
Expand Down

0 comments on commit 07ebd21

Please sign in to comment.