Change email node module

This commit is contained in:
Jim Laurie 2017-11-16 18:00:15 +01:00
parent a512d232d3
commit cd834d6828
6 changed files with 64 additions and 44 deletions

View File

@ -1,10 +0,0 @@
{
"smtp": {
"from": "<no-reply@website.com>",
"service": {
"name": "",
"user": "",
"pass": ""
}
}
}

View File

@ -1,10 +0,0 @@
{
"smtp": {
"from": "<no-reply@website.com>",
"service": {
"name": "",
"user": "",
"pass": ""
}
}
}

View File

@ -23,9 +23,6 @@
"prepublish": "npm run build", "prepublish": "npm run build",
"postinstall": "node node_modules/strapi-helper-plugin/lib/internals/scripts/postinstall.js" "postinstall": "node node_modules/strapi-helper-plugin/lib/internals/scripts/postinstall.js"
}, },
"dependencies": {
"nodemailer": "^4.4.0"
},
"devDependencies": { "devDependencies": {
"cross-env": "^5.1.1", "cross-env": "^5.1.1",
"eslint": "^4.11.0", "eslint": "^4.11.0",
@ -60,5 +57,8 @@
"node": ">= 7.0.0", "node": ">= 7.0.0",
"npm": ">= 3.0.0" "npm": ">= 3.0.0"
}, },
"license": "MIT" "license": "MIT",
"dependencies": {
"sendmail": "^1.2.0"
}
} }

View File

@ -7,37 +7,22 @@
*/ */
const _ = require('lodash'); const _ = require('lodash');
const nodemailer = require('nodemailer'); const sendmail = require('sendmail')({
silent: true
});
module.exports = { module.exports = {
send: (options, cb) => { send: (options, cb) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
try { try {
const config = strapi.plugins['email'].config[strapi.config.environment];
// Format transport config.
let transportConfig;
if (config.smtp && config.smtp.service && config.smtp.service.name) {
transportConfig = {
service: config.smtp.service.name,
auth: {
user: config.smtp.service.user,
pass: config.smtp.service.pass
}
};
}
// Init the transporter.
const transporter = nodemailer.createTransport(transportConfig);
// Default values. // Default values.
options = _.isObject(options) ? options : {}; options = _.isObject(options) ? options : {};
options.from = config.smtp.from || ''; options.from = 'admin@strapiapp.com';
options.text = options.text || options.html; options.text = options.text || options.html;
options.html = options.html || options.text; options.html = options.html || options.text;
// Send the email. // Send the email.
transporter.sendMail({ sendmail({
from: options.from, from: options.from,
to: options.to, to: options.to,
subject: options.subject, subject: options.subject,

View File

@ -51,6 +51,15 @@
"prefix": "" "prefix": ""
} }
}, },
{
"method": "POST",
"path": "/auth/forgot-password",
"handler": "Auth.forgotPassword",
"config": {
"policies": [],
"prefix": ""
}
},
{ {
"method": "GET", "method": "GET",

View File

@ -7,6 +7,7 @@
*/ */
const _ = require('lodash'); const _ = require('lodash');
const crypto = require('crypto');
module.exports = { module.exports = {
callback: async (ctx) => { callback: async (ctx) => {
@ -146,5 +147,50 @@ module.exports = {
message: err.message message: err.message
}; };
} }
},
forgotPassword: async (ctx) => {
const email = ctx.request.body.email;
const url = ctx.request.body.url;
// Find the user user thanks to his email.
const user = await strapi.query('user', 'users-permissions').findOne({ email });
// User not found.
if (!user) {
ctx.status = 400;
return ctx.body = {
message: 'This email does not exist.'
};
}
// Generate random token.
const resetPasswordToken = crypto.randomBytes(64).toString('hex');
// Set the property code of the local passport.
user.resetPasswordToken = resetPasswordToken;
// Update the user.
await strapi.query('user', 'users-permissions').update({
id: user.id,
values: user
});
// Send an email to the user.
try {
await strapi.plugins['email'].services.email.send({
to: user.email,
subject: 'Reset password',
text: url + '?code=' + resetPasswordToken,
html: url + '?code=' + resetPasswordToken
});
ctx.status = 200;
ctx.body = {};
} catch (err) {
ctx.status = 500;
ctx.body = {
message: 'Error sending the email'
};
}
} }
}; };