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:
parent
01ce9b5f5a
commit
a22b1e13f1
|
@ -1,6 +1,7 @@
|
||||||
.idea
|
.idea
|
||||||
.project
|
.project
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
.vscode/
|
||||||
*.sublime*
|
*.sublime*
|
||||||
*.seed
|
*.seed
|
||||||
*.log
|
*.log
|
||||||
|
|
|
@ -450,6 +450,7 @@ module.exports = function(User) {
|
||||||
|
|
||||||
// Set a default token generation function if one is not provided
|
// Set a default token generation function if one is not provided
|
||||||
var tokenGenerator = options.generateVerificationToken || User.generateVerificationToken;
|
var tokenGenerator = options.generateVerificationToken || User.generateVerificationToken;
|
||||||
|
assert(typeof tokenGenerator === 'function', 'generateVerificationToken must be a function');
|
||||||
|
|
||||||
tokenGenerator(user, function(err, token) {
|
tokenGenerator(user, function(err, token) {
|
||||||
if (err) { return fn(err); }
|
if (err) { return fn(err); }
|
||||||
|
@ -468,6 +469,8 @@ module.exports = function(User) {
|
||||||
function sendEmail(user) {
|
function sendEmail(user) {
|
||||||
options.verifyHref += '&token=' + user.verificationToken;
|
options.verifyHref += '&token=' + user.verificationToken;
|
||||||
|
|
||||||
|
options.verificationToken = user.verificationToken;
|
||||||
|
|
||||||
options.text = options.text || g.f('Please verify your email by opening ' +
|
options.text = options.text || g.f('Please verify your email by opening ' +
|
||||||
'this link in a web browser:\n\t%s', options.verifyHref);
|
'this link in a web browser:\n\t%s', options.verifyHref);
|
||||||
|
|
||||||
|
|
|
@ -1714,6 +1714,27 @@ describe('User', function() {
|
||||||
.to.equal('#/some-path?a=1&b=2');
|
.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() {
|
describe('User.confirm(options, fn)', function() {
|
||||||
|
|
Loading…
Reference in New Issue