Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions common/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,10 @@ module.exports = function(User) {
* @property {String} text Text of email.
* @property {String} template Name of template that displays verification
* page, for example, `'verify.ejs'.
* @property {Function} templateFn A function generating the email HTML body
* from `verify()` options object and generated attributes like `options.verifyHref`.
* It must accept the option object and a callback function with `(err, html)`
* as parameters
* @property {String} redirect Page to which user will be redirected after
* they verify their email, for example `'/'` for root URI.
* @property {Function} generateVerificationToken A function to be used to
Expand Down Expand Up @@ -418,6 +422,8 @@ module.exports = function(User) {
'&redirect=' +
options.redirect;

options.templateFn = options.templateFn || createVerificationEmailBody;

// Email model
var Email = options.mailer || this.constructor.email || registry.getModelByType(loopback.Email);

Expand Down Expand Up @@ -452,20 +458,35 @@ module.exports = function(User) {

options.headers = options.headers || {};

var template = loopback.template(options.template);
options.html = template(options);

Email.send(options, function(err, email) {
options.templateFn(options, function(err, html) {
if (err) {
fn(err);
} else {
fn(null, {email: email, token: user.verificationToken, uid: user.id});
setHtmlContentAndSend(html);
}
});

function setHtmlContentAndSend(html) {
options.html = html;

Email.send(options, function(err, email) {
if (err) {
fn(err);
} else {
fn(null, { email: email, token: user.verificationToken, uid: user.id });
}
});
}
}
return fn.promise;
};

function createVerificationEmailBody(options, cb) {
var template = loopback.template(options.template);
var body = template(options);
cb(null, body);
}

/**
* A default verification token generator which accepts the user the token is
* being generated for and a callback function to indicate completion.
Expand Down
39 changes: 39 additions & 0 deletions test/user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1364,6 +1364,45 @@ describe('User', function() {
});
});

it('Verify a user\'s email address with custom template function', function(done) {
User.afterRemote('create', function(ctx, user, next) {
assert(user, 'afterRemote should include result');

var options = {
type: 'email',
to: user.email,
from: 'noreply@myapp.org',
redirect: '/',
protocol: ctx.req.protocol,
host: ctx.req.get('host'),
templateFn: function(options, cb) {
cb(null, 'custom template - verify url: ' + options.verifyHref);
},
};

user.verify(options, function(err, result) {
assert(result.email);
assert(result.email.response);
assert(result.token);
var msg = result.email.response.toString('utf-8');
assert(~msg.indexOf('/api/test-users/confirm'));
assert(~msg.indexOf('custom template'));
assert(~msg.indexOf('To: bar@bat.com'));

done();
});
});

request(app)
.post('/test-users')
.expect('Content-Type', /json/)
.expect(200)
.send({ email: 'bar@bat.com', password: 'bar' })
.end(function(err, res) {
if (err) return done(err);
});
});

it('Verify a user\'s email address with custom token generator', function(done) {
User.afterRemote('create', function(ctx, user, next) {
assert(user, 'afterRemote should include result');
Expand Down