Skip to content

Commit 9176ee2

Browse files
authored
Merge pull request #3609 from sebastianfelipe/fix/user-verify-duplicated-token
Fix handling of user verification options
2 parents 8488da2 + d0a4941 commit 9176ee2

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

common/models/user.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -603,11 +603,11 @@ module.exports = function(User) {
603603
*/
604604

605605
User.getVerifyOptions = function() {
606-
const verifyOptions = {
606+
const defaultOptions = {
607607
type: 'email',
608608
609609
};
610-
return this.settings.verifyOptions || verifyOptions;
610+
return Object.assign({}, this.settings.verifyOptions || defaultOptions);
611611
};
612612

613613
/**
@@ -699,11 +699,15 @@ module.exports = function(User) {
699699
var user = this;
700700
var userModel = this.constructor;
701701
var registry = userModel.registry;
702-
702+
verifyOptions = Object.assign({}, verifyOptions);
703703
// final assertion is performed once all options are assigned
704704
assert(typeof verifyOptions === 'object',
705705
'verifyOptions object param required when calling user.verify()');
706706

707+
// Shallow-clone the options object so that we don't override
708+
// the global default options object
709+
verifyOptions = Object.assign({}, verifyOptions);
710+
707711
// Set a default template generation function if none provided
708712
verifyOptions.templateFn = verifyOptions.templateFn || createVerificationEmailBody;
709713

test/user.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,6 +1606,34 @@ describe('User', function() {
16061606
done();
16071607
});
16081608

1609+
it('returns same verifyOptions after verify user model', () => {
1610+
const defaultOptions = {
1611+
type: 'email',
1612+
1613+
};
1614+
var verifyOptions = Object.assign({}, defaultOptions);
1615+
const user = new User({
1616+
1617+
password: 'pass',
1618+
verificationToken: 'example-token',
1619+
});
1620+
return user
1621+
.verify(verifyOptions)
1622+
.then(res => expect(verifyOptions).to.eql(defaultOptions));
1623+
});
1624+
1625+
it('getVerifyOptions() always returns the same', () => {
1626+
const defaultOptions = {
1627+
type: 'email',
1628+
1629+
};
1630+
User.settings.verifyOptions = Object.assign({}, defaultOptions);
1631+
var verifyOptions = User.getVerifyOptions();
1632+
verifyOptions.from = '[email protected]';
1633+
verifyOptions.test = 'test';
1634+
expect(User.getVerifyOptions()).to.eql(defaultOptions);
1635+
});
1636+
16091637
it('can be extended by user', function(done) {
16101638
User.getVerifyOptions = function() {
16111639
const base = User.base.getVerifyOptions();

0 commit comments

Comments
 (0)