From cabe546ecf393c9afda6008dbb5f357b4be6203c Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Thu, 5 Jan 2017 17:19:38 -0700 Subject: [PATCH] Added email send on new user if enabled for confirming --- routes/api/users/index.js | 41 +++++++++++++++++++++++++++++-- views/email/email-confirm.ejs | 3 +++ views/email/email-confirm.txt.ejs | 9 +++++++ 3 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 views/email/email-confirm.ejs create mode 100644 views/email/email-confirm.txt.ejs diff --git a/routes/api/users/index.js b/routes/api/users/index.js index 8dc48d5dc..8f1bf7d41 100644 --- a/routes/api/users/index.js +++ b/routes/api/users/index.js @@ -1,6 +1,8 @@ const express = require('express'); const router = express.Router(); const User = require('../../../models/user'); +const Setting = require('../../../models/setting'); +const mailer = require('../../../services/mailer'); const authorization = require('../../../middleware/authorization'); router.get('/', authorization.needed('admin'), (req, res, next) => { @@ -59,8 +61,43 @@ router.post('/', authorization.needed('admin'), (req, res, next) => { User .createLocalUser(email, password, displayName) - .then(user => { - res.status(201).json(user); + .then((user) => { + + // Get the settings from the database to find out if we need to send an + // email confirmation. The Front end will know about the + // requireEmailConfirmation as it's included in the settings get endpoint. + return Setting.retrieve().then(({requireEmailConfirmation = false}) => { + + if (requireEmailConfirmation) { + + // Email confirmation is required, let's generate that token and send + // the email. + return User + .createEmailConfirmToken(user.id, email) + .then((token) => { + return mailer.sendSimple({ + app: req.app, // needed to render the templates. + template: 'email/email-confirm', // needed to know which template to render! + locals: { // specifies the template locals. + token, + rootURL: process.env.TALK_ROOT_URL, + email + }, + subject: 'Email Confirmation - Talk', + to: email + }); + }) + .then(() => { + + // Then send back the user. + res.status(201).json(user); + }); + } else { + + // We don't need to confirm the email, let's just send back the user! + res.status(201).json(user); + } + }); }) .catch(err => { next(err); diff --git a/views/email/email-confirm.ejs b/views/email/email-confirm.ejs new file mode 100644 index 000000000..da1b0e815 --- /dev/null +++ b/views/email/email-confirm.ejs @@ -0,0 +1,3 @@ +

A email confirmation has been requested for the following account: <%= email %>.

+

To confirm the account, please visit the following link: http://example.com/email/confirm/endpoint#<%= token %>

+

If you did not request this, you can safely ignore this email.

diff --git a/views/email/email-confirm.txt.ejs b/views/email/email-confirm.txt.ejs new file mode 100644 index 000000000..764991d8d --- /dev/null +++ b/views/email/email-confirm.txt.ejs @@ -0,0 +1,9 @@ +A email confirmation has been requested for the following account: + + <%= email %> + +To confirm the account, please visit the following link: + + http://example.com/email/confirm/endpoint#<%= token %> + +If you did not request this, you can safely ignore this email.