Merge pull request #2938 from strongloop/feature/verify-template-fn-2x
Add templateFn option to User#verify()
This commit is contained in:
commit
586fa1cebb
|
@ -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
|
||||
|
@ -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);
|
||||
|
||||
|
@ -452,8 +458,16 @@ module.exports = function(User) {
|
|||
|
||||
options.headers = options.headers || {};
|
||||
|
||||
var template = loopback.template(options.template);
|
||||
options.html = template(options);
|
||||
options.templateFn(options, function(err, html) {
|
||||
if (err) {
|
||||
fn(err);
|
||||
} else {
|
||||
setHtmlContentAndSend(html);
|
||||
}
|
||||
});
|
||||
|
||||
function setHtmlContentAndSend(html) {
|
||||
options.html = html;
|
||||
|
||||
Email.send(options, function(err, email) {
|
||||
if (err) {
|
||||
|
@ -463,9 +477,16 @@ module.exports = function(User) {
|
|||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
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.
|
||||
|
|
|
@ -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');
|
||||
|
|
Loading…
Reference in New Issue