wip: implement sendVerificationEmail

This commit is contained in:
Elliot DeNolf
2020-09-16 10:47:21 -04:00
parent a39cec2b76
commit c439297223
3 changed files with 55 additions and 3 deletions

View File

@@ -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:
<a href="${url}">
${url}
</a>
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;

View File

@@ -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);

View File

@@ -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: {