Skip to content

Commit e07feb8

Browse files
authored
Merge pull request #2937 from strongloop/feature/verify-template-fn
Add templateFn option to User#verify()
2 parents d85ff32 + 85da50c commit e07feb8

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

common/models/user.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,10 @@ module.exports = function(User) {
364364
* @property {String} text Text of email.
365365
* @property {String} template Name of template that displays verification
366366
* page, for example, `'verify.ejs'.
367+
* @property {Function} templateFn A function generating the email HTML body
368+
* from `verify()` options object and generated attributes like `options.verifyHref`.
369+
* It must accept the option object and a callback function with `(err, html)`
370+
* as parameters
367371
* @property {String} redirect Page to which user will be redirected after
368372
* they verify their email, for example `'/'` for root URI.
369373
* @property {Function} generateVerificationToken A function to be used to
@@ -424,6 +428,8 @@ module.exports = function(User) {
424428
'&redirect=' +
425429
options.redirect;
426430

431+
options.templateFn = options.templateFn || createVerificationEmailBody;
432+
427433
// Email model
428434
var Email = options.mailer || this.constructor.email || registry.getModelByType(loopback.Email);
429435

@@ -458,20 +464,36 @@ module.exports = function(User) {
458464

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

461-
var template = loopback.template(options.template);
462-
options.html = template(options);
463-
464-
Email.send(options, function(err, email) {
467+
options.templateFn(options, function(err, html) {
465468
if (err) {
466469
fn(err);
467470
} else {
468-
fn(null, { email: email, token: user.verificationToken, uid: user.id });
471+
setHtmlContentAndSend(html);
469472
}
470473
});
474+
475+
function setHtmlContentAndSend(html) {
476+
options.html = html;
477+
478+
Email.send(options, function(err, email) {
479+
if (err) {
480+
fn(err);
481+
} else {
482+
fn(null, { email: email, token: user.verificationToken, uid: user.id });
483+
}
484+
});
485+
}
471486
}
472487
return fn.promise;
473488
};
474489

490+
function createVerificationEmailBody(options, cb) {
491+
var template = loopback.template(options.template);
492+
var body = template(options);
493+
cb(null, body);
494+
}
495+
496+
475497
/**
476498
* A default verification token generator which accepts the user the token is
477499
* being generated for and a callback function to indicate completion.

test/user.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,6 +1365,45 @@ describe('User', function() {
13651365
});
13661366
});
13671367

1368+
it('Verify a user\'s email address with custom template function', function(done) {
1369+
User.afterRemote('create', function(ctx, user, next) {
1370+
assert(user, 'afterRemote should include result');
1371+
1372+
var options = {
1373+
type: 'email',
1374+
to: user.email,
1375+
1376+
redirect: '/',
1377+
protocol: ctx.req.protocol,
1378+
host: ctx.req.get('host'),
1379+
templateFn: function(options, cb) {
1380+
cb(null, 'custom template - verify url: ' + options.verifyHref);
1381+
},
1382+
};
1383+
1384+
user.verify(options, function(err, result) {
1385+
assert(result.email);
1386+
assert(result.email.response);
1387+
assert(result.token);
1388+
var msg = result.email.response.toString('utf-8');
1389+
assert(~msg.indexOf('/api/test-users/confirm'));
1390+
assert(~msg.indexOf('custom template'));
1391+
assert(~msg.indexOf('To: [email protected]'));
1392+
1393+
done();
1394+
});
1395+
});
1396+
1397+
request(app)
1398+
.post('/test-users')
1399+
.expect('Content-Type', /json/)
1400+
.expect(200)
1401+
.send({ email: '[email protected]', password: 'bar' })
1402+
.end(function(err, res) {
1403+
if (err) return done(err);
1404+
});
1405+
});
1406+
13681407
it('Verify a user\'s email address with custom token generator', function(done) {
13691408
User.afterRemote('create', function(ctx, user, next) {
13701409
assert(user, 'afterRemote should include result');

0 commit comments

Comments
 (0)