From c43929722326b6d84b059c258a266fc363a8a680 Mon Sep 17 00:00:00 2001 From: Elliot DeNolf Date: Wed, 16 Sep 2020 10:47:21 -0400 Subject: [PATCH] wip: implement sendVerificationEmail --- src/auth/sendVerificationEmail.js | 37 ++++++++++++++++++++++++++++ src/collections/operations/create.js | 19 +++++++++++--- src/init/bindOperations.js | 2 ++ 3 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 src/auth/sendVerificationEmail.js diff --git a/src/auth/sendVerificationEmail.js b/src/auth/sendVerificationEmail.js new file mode 100644 index 0000000000..94058ee5b5 --- /dev/null +++ b/src/auth/sendVerificationEmail.js @@ -0,0 +1,37 @@ +async function sendVerificationEmail(args) { + const { config, sendEmail: email } = this; + + const options = { ...args }; + + // Verify token from e-mail + const { + collection: { + config: collectionConfig, + }, + user, + data, + disableEmail, + req, + } = options; + + if (!disableEmail) { + const url = collectionConfig.auth.generateVerificationUrl + ? `${config.serverURL}${collectionConfig.auth.generateVerificationUrl(req, user.verificationToken)}` + : 'asdfasdf'; // TODO: point to payload view that verifies + const html = `Thank you for created an account. + Please click on the following link, or paste this into your browser to complete the process: + + ${url} + + If you did not request this, please ignore this email and your password will remain unchanged.`; + + email({ + from: `"${config.email.fromName}" <${config.email.fromAddress}>`, + to: data.email, + subject: 'Verify Email', + html, + }); + } +} + +module.exports = sendVerificationEmail; diff --git a/src/collections/operations/create.js b/src/collections/operations/create.js index 00c86edc5a..841e738f6b 100644 --- a/src/collections/operations/create.js +++ b/src/collections/operations/create.js @@ -9,6 +9,8 @@ const getSafeFilename = require('../../uploads/getSafeFilename'); const getImageSize = require('../../uploads/getImageSize'); const imageMIMETypes = require('../../uploads/imageMIMETypes'); +const sendVerificationEmail = require('../../auth/sendVerificationEmail'); + async function create(args) { const { performFieldOperations } = this; @@ -145,8 +147,7 @@ async function create(args) { } if (collectionConfig.auth.emailVerification) { data.verified = false; - data.verificationToken = await crypto.randomBytes(20).toString('hex'); - // TODO: Generate and send email + data.verificationToken = crypto.randomBytes(20).toString('hex'); } } @@ -188,7 +189,19 @@ async function create(args) { }, Promise.resolve()); // ///////////////////////////////////// - // 10. Return results + // 10. Send verification email if applicable + // ///////////////////////////////////// + + if (collectionConfig.auth.emailVerification) { + await sendVerificationEmail({ + collection: { config: collectionConfig, Model }, + user: result, + req, + }); + } + + // ///////////////////////////////////// + // 11. Return results // ///////////////////////////////////// result = JSON.stringify(result); diff --git a/src/init/bindOperations.js b/src/init/bindOperations.js index ed1bd032f9..d45e21f114 100644 --- a/src/init/bindOperations.js +++ b/src/init/bindOperations.js @@ -7,6 +7,7 @@ const me = require('../auth/operations/me'); const refresh = require('../auth/operations/refresh'); const registerFirstUser = require('../auth/operations/registerFirstUser'); const resetPassword = require('../auth/operations/resetPassword'); +const sendVerificationEmail = require('../auth/sendVerificationEmail'); const create = require('../collections/operations/create'); const find = require('../collections/operations/find'); @@ -37,6 +38,7 @@ function bindOperations(ctx) { refresh: refresh.bind(ctx), registerFirstUser: registerFirstUser.bind(ctx), resetPassword: resetPassword.bind(ctx), + sendVerificationEmail: sendVerificationEmail.bind(ctx), }, }, globals: {