Skip to content

Commit 5c1558f

Browse files
adrien-kbajtos
authored andcommitted
Add templateFn option to User#verify()
1 parent d06190d commit 5c1558f

File tree

2 files changed

+65
-5
lines changed

2 files changed

+65
-5
lines changed

common/models/user.js

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

425+
options.templateFn = options.templateFn || createVerificationEmailBody;
426+
421427
// Email model
422428
var Email = options.mailer || this.constructor.email || registry.getModelByType(loopback.Email);
423429

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

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

455-
var template = loopback.template(options.template);
456-
options.html = template(options);
457-
458-
Email.send(options, function(err, email) {
461+
options.templateFn(options, function(err, html) {
459462
if (err) {
460463
fn(err);
461464
} else {
462-
fn(null, {email: email, token: user.verificationToken, uid: user.id});
465+
setHtmlContentAndSend(html);
463466
}
464467
});
468+
469+
function setHtmlContentAndSend(html) {
470+
options.html = html;
471+
472+
Email.send(options, function(err, email) {
473+
if (err) {
474+
fn(err);
475+
} else {
476+
fn(null, { email: email, token: user.verificationToken, uid: user.id });
477+
}
478+
});
479+
}
465480
}
466481
return fn.promise;
467482
};
468483

484+
function createVerificationEmailBody(options, cb) {
485+
var template = loopback.template(options.template);
486+
var body = template(options);
487+
cb(null, body);
488+
}
489+
469490
/**
470491
* A default verification token generator which accepts the user the token is
471492
* 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
@@ -1364,6 +1364,45 @@ describe('User', function() {
13641364
});
13651365
});
13661366

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

0 commit comments

Comments
 (0)