Skip to content

Commit c6d8565

Browse files
authored
Merge pull request #3896 from wolrajhti/patch-2
Fix bug in User.verify when confirm is disabled
2 parents eb3301a + cc4fc21 commit c6d8565

File tree

2 files changed

+50
-16
lines changed

2 files changed

+50
-16
lines changed

common/models/user.js

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -736,22 +736,32 @@ module.exports = function(User) {
736736
(verifyOptions.protocol === 'https' && verifyOptions.port == '443')
737737
) ? '' : ':' + verifyOptions.port;
738738

739-
var urlPath = joinUrlPath(
740-
verifyOptions.restApiRoot,
741-
userModel.http.path,
742-
userModel.sharedClass.findMethodByName('confirm').http.path
743-
);
739+
if (!verifyOptions.verifyHref) {
740+
const confirmMethod = userModel.sharedClass.findMethodByName('confirm');
741+
if (!confirmMethod) {
742+
throw new Error(
743+
'Cannot build user verification URL, ' +
744+
'the default confirm method is not public. ' +
745+
'Please provide the URL in verifyOptions.verifyHref.');
746+
}
744747

745-
verifyOptions.verifyHref = verifyOptions.verifyHref ||
746-
verifyOptions.protocol +
747-
'://' +
748-
verifyOptions.host +
749-
displayPort +
750-
urlPath +
751-
'?' + qs.stringify({
752-
uid: '' + verifyOptions.user[pkName],
753-
redirect: verifyOptions.redirect,
754-
});
748+
const urlPath = joinUrlPath(
749+
verifyOptions.restApiRoot,
750+
userModel.http.path,
751+
confirmMethod.http.path
752+
);
753+
754+
verifyOptions.verifyHref =
755+
verifyOptions.protocol +
756+
'://' +
757+
verifyOptions.host +
758+
displayPort +
759+
urlPath +
760+
'?' + qs.stringify({
761+
uid: '' + verifyOptions.user[pkName],
762+
redirect: verifyOptions.redirect,
763+
});
764+
}
755765

756766
verifyOptions.to = verifyOptions.to || user.email;
757767
verifyOptions.subject = verifyOptions.subject || g.f('Thanks for Registering');
@@ -779,7 +789,10 @@ module.exports = function(User) {
779789

780790
// TODO - support more verification types
781791
function sendEmail(user) {
782-
verifyOptions.verifyHref += '&token=' + user.verificationToken;
792+
verifyOptions.verifyHref +=
793+
verifyOptions.verifyHref.indexOf('?') === -1 ? '?' : '&';
794+
verifyOptions.verifyHref += 'token=' + user.verificationToken;
795+
783796
verifyOptions.verificationToken = user.verificationToken;
784797
verifyOptions.text = verifyOptions.text || g.f('Please verify your email by opening ' +
785798
'this link in a web browser:\n\t%s', verifyOptions.verifyHref);

test/user.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2155,6 +2155,27 @@ describe('User', function() {
21552155
});
21562156
});
21572157

2158+
it('handles the case when remote method "confirm" is disabled', () => {
2159+
let actualVerifyHref;
2160+
const VERIFY_HREF = 'http://example.com/a-verify-url';
2161+
2162+
Object.assign(verifyOptions, {
2163+
verifyHref: VERIFY_HREF,
2164+
templateFn: (options, cb) => {
2165+
actualVerifyHref = options.verifyHref;
2166+
cb(null, 'dummy body');
2167+
},
2168+
});
2169+
2170+
User.disableRemoteMethodByName('confirm');
2171+
2172+
return user.verify(verifyOptions)
2173+
.then(() => {
2174+
expect(actualVerifyHref.substring(0, VERIFY_HREF.length + 1))
2175+
.to.equal(`${VERIFY_HREF}?`);
2176+
});
2177+
});
2178+
21582179
function givenUser() {
21592180
return User.create({email: '[email protected]', password: 'pass'})
21602181
.then(u => user = u);

0 commit comments

Comments
 (0)