Pass options.verificationToken to templateFn

Enhance User.prototype.verify to pass the generated token
to the templating function in addition to other existing properties.

This allows application to build multiple URLs in the email template,
for example in order to provide a different URL for desktop and
mobile browsers.
This commit is contained in:
Hiran del Castillo 2017-03-03 23:45:02 -05:00 committed by Miroslav Bajtoš
parent 01ce9b5f5a
commit a22b1e13f1
No known key found for this signature in database
GPG Key ID: 797723F23CE0A94A
3 changed files with 25 additions and 0 deletions

1
.gitignore vendored
View File

@ -1,6 +1,7 @@
.idea
.project
.DS_Store
.vscode/
*.sublime*
*.seed
*.log

View File

@ -450,6 +450,7 @@ module.exports = function(User) {
// Set a default token generation function if one is not provided
var tokenGenerator = options.generateVerificationToken || User.generateVerificationToken;
assert(typeof tokenGenerator === 'function', 'generateVerificationToken must be a function');
tokenGenerator(user, function(err, token) {
if (err) { return fn(err); }
@ -468,6 +469,8 @@ module.exports = function(User) {
function sendEmail(user) {
options.verifyHref += '&token=' + user.verificationToken;
options.verificationToken = user.verificationToken;
options.text = options.text || g.f('Please verify your email by opening ' +
'this link in a web browser:\n\t%s', options.verifyHref);

View File

@ -1714,6 +1714,27 @@ describe('User', function() {
.to.equal('#/some-path?a=1&b=2');
});
});
it('verify that options.templateFn receives options.verificationToken', function() {
return User.create({email: 'test1@example.com', password: 'pass'})
.then(user => {
let actualVerificationToken;
return user.verify({
type: 'email',
to: user.email,
from: 'noreply@myapp.org',
redirect: '#/some-path?a=1&b=2',
templateFn: (options, cb) => {
actualVerificationToken = options.verificationToken;
cb(null, 'dummy body');
},
})
.then(() => actualVerificationToken);
})
.then(token => {
expect(token).to.exist();
});
});
});
describe('User.confirm(options, fn)', function() {