Added email send on new user if enabled for confirming

This commit is contained in:
Wyatt Johnson
2017-01-05 17:19:38 -07:00
parent be72ebf2a1
commit cabe546ecf
3 changed files with 51 additions and 2 deletions
+39 -2
View File
@@ -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);
+3
View File
@@ -0,0 +1,3 @@
<p>A email confirmation has been requested for the following account: <b><%= email %></b>.</p>
<p>To confirm the account, please visit the following link: <a href="http://example.com/email/confirm/endpoint#<%= token %>">http://example.com/email/confirm/endpoint#<%= token %></a></p>
<p>If you did not request this, you can safely ignore this email.</p>
+9
View File
@@ -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.