Skip to content

Commit 3eb9009

Browse files
authored
Merge pull request #2738 from strongloop/fix/user-verify-email-with-empty-rest-root
Fix double-slash in confirmation URL
2 parents eec8536 + 21ff383 commit 3eb9009

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

common/models/user.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,14 +408,18 @@ module.exports = function(User) {
408408
(options.protocol === 'https' && options.port == '443')
409409
) ? '' : ':' + options.port;
410410

411+
var urlPath = joinUrlPath(
412+
options.restApiRoot,
413+
userModel.http.path,
414+
userModel.sharedClass.findMethodByName('confirm').http.path
415+
);
416+
411417
options.verifyHref = options.verifyHref ||
412418
options.protocol +
413419
'://' +
414420
options.host +
415421
displayPort +
416-
options.restApiRoot +
417-
userModel.http.path +
418-
userModel.sharedClass.findMethodByName('confirm').http.path +
422+
urlPath +
419423
'?uid=' +
420424
options.user.id +
421425
'&redirect=' +
@@ -783,3 +787,13 @@ function emailValidator(err, done) {
783787
if (!isEmail(value))
784788
return err('email');
785789
}
790+
791+
function joinUrlPath(args) {
792+
var result = arguments[0];
793+
for (var ix = 1; ix < arguments.length; ix++) {
794+
var next = arguments[ix];
795+
result += result[result.length - 1] === '/' && next[0] === '/' ?
796+
next.slice(1) : next;
797+
}
798+
return result;
799+
}

test/user.test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,6 +1547,41 @@ describe('User', function() {
15471547

15481548
done();
15491549
});
1550+
1551+
it('should squash "//" when restApiRoot is "/"', function(done) {
1552+
var emailBody;
1553+
User.afterRemote('create', function(ctx, user, next) {
1554+
assert(user, 'afterRemote should include result');
1555+
1556+
var options = {
1557+
type: 'email',
1558+
to: user.email,
1559+
1560+
redirect: '/',
1561+
host: 'myapp.org',
1562+
port: 3000,
1563+
restApiRoot: '/',
1564+
};
1565+
1566+
user.verify(options, function(err, result) {
1567+
if (err) return next(err);
1568+
emailBody = result.email.response.toString('utf-8');
1569+
next();
1570+
});
1571+
});
1572+
1573+
request(app)
1574+
.post('/test-users')
1575+
.expect('Content-Type', /json/)
1576+
.expect(200)
1577+
.send({ email: '[email protected]', password: 'pass' })
1578+
.end(function(err, res) {
1579+
if (err) return done(err);
1580+
expect(emailBody)
1581+
.to.contain('http://myapp.org:3000/test-users/confirm');
1582+
done();
1583+
});
1584+
});
15501585
});
15511586

15521587
describe('User.confirm(options, fn)', function() {

0 commit comments

Comments
 (0)