Merge pull request #2938 from strongloop/feature/verify-template-fn-2x

Add templateFn option to User#verify()
This commit is contained in:
Miroslav Bajtoš 2016-11-15 14:17:32 +01:00 committed by GitHub
commit 586fa1cebb
2 changed files with 65 additions and 5 deletions

View File

@ -365,6 +365,10 @@ module.exports = function(User) {
* @property {String} text Text of email. * @property {String} text Text of email.
* @property {String} template Name of template that displays verification * @property {String} template Name of template that displays verification
* page, for example, `'verify.ejs'. * 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 * @property {String} redirect Page to which user will be redirected after
* they verify their email, for example `'/'` for root URI. * they verify their email, for example `'/'` for root URI.
* @property {Function} generateVerificationToken A function to be used to * @property {Function} generateVerificationToken A function to be used to
@ -418,6 +422,8 @@ module.exports = function(User) {
'&redirect=' + '&redirect=' +
options.redirect; options.redirect;
options.templateFn = options.templateFn || createVerificationEmailBody;
// Email model // Email model
var Email = options.mailer || this.constructor.email || registry.getModelByType(loopback.Email); var Email = options.mailer || this.constructor.email || registry.getModelByType(loopback.Email);
@ -452,20 +458,35 @@ module.exports = function(User) {
options.headers = options.headers || {}; options.headers = options.headers || {};
var template = loopback.template(options.template); options.templateFn(options, function(err, html) {
options.html = template(options);
Email.send(options, function(err, email) {
if (err) { if (err) {
fn(err); fn(err);
} else { } 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; 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 * A default verification token generator which accepts the user the token is
* being generated for and a callback function to indicate completion. * being generated for and a callback function to indicate completion.

View File

@ -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) { it('Verify a user\'s email address with custom token generator', function(done) {
User.afterRemote('create', function(ctx, user, next) { User.afterRemote('create', function(ctx, user, next) {
assert(user, 'afterRemote should include result'); assert(user, 'afterRemote should include result');