From 567e2530d77b4a23b1456303950c0ac57ced8547 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Sat, 26 Jul 2014 22:39:42 -0700 Subject: [PATCH] Build the email verification url from app context https://github.com/strongloop/loopback/issues/408 --- lib/models/user.js | 14 +++++++++++--- test/user.test.js | 4 +++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/models/user.js b/lib/models/user.js index a6e413fd..09e3281a 100644 --- a/lib/models/user.js +++ b/lib/models/user.js @@ -277,6 +277,7 @@ User.prototype.hasPassword = function (plain, fn) { User.prototype.verify = function (options, fn) { var user = this; + var userModel = this.constructor; assert(typeof options === 'object', 'options required when calling user.verify()'); assert(options.type, 'You must supply a verification type (options.type)'); assert(options.type === 'email', 'Unsupported verification type'); @@ -287,13 +288,20 @@ User.prototype.verify = function (options, fn) { options.template = path.resolve(options.template || path.join(__dirname, '..', '..', 'templates', 'verify.ejs')); options.user = this; options.protocol = options.protocol || 'http'; - options.host = options.host || 'localhost'; + + var app = this.app; + options.host = options.host || (app && app.get('host')) || 'localhost'; + options.port = options.port || (app && app.get('port')) || 3000; + options.restApiRoot = options.restApiRoot || (app && app.get('restApiRoot')) || '/api'; options.verifyHref = options.verifyHref || options.protocol + '://' + options.host - + User.http.path - + User.confirm.http.path + + ':' + + options.port + + options.restApiRoot + + userModel.http.path + + userModel.confirm.http.path + '?uid=' + options.user.id + '&redirect=' diff --git a/test/user.test.js b/test/user.test.js index b7588779..0bfc9240 100644 --- a/test/user.test.js +++ b/test/user.test.js @@ -454,7 +454,9 @@ describe('User', function(){ assert(result.email); assert(result.email.response); assert(result.token); - assert(~result.email.response.toString('utf-8').indexOf('To: bar@bat.com')); + var msg = result.email.response.toString('utf-8'); + assert(~msg.indexOf('/api/users/confirm')); + assert(~msg.indexOf('To: bar@bat.com')); done(); }); });