Repository: auth0/auth0.js
Pull request #267 (2016-12-13 19:05:38)

Add get user country method for passwordless


Patch:
diff --git a/src/authentication/index.js b/src/authentication/index.js
index fe52320..c0921a3 100644
--- a/src/authentication/index.js
+++ b/src/authentication/index.js
@@ -369,4 +369,22 @@ Authentication.prototype.delegation = function (options, cb) {
     .end(responseHandler(cb));
 };
 
+/**
+ * Fetches the user country based on the ip.
+ *
+ * @method getUserCountry
+ * @param {Function} cb
+ */
+Authentication.prototype.getUserCountry = function (cb) {
+  var url;
+
+  assert.check(cb, { type: 'function', message: 'cb parameter is not valid' });
+
+  url = urljoin(this.baseOptions.rootUrl, 'user', 'geoloc', 'country');
+
+  return this.request
+    .get(url)
+    .end(responseHandler(cb));
+};
+
 module.exports = Authentication;
diff --git a/src/helper/response-handler.js b/src/helper/response-handler.js
index 9b75d0a..929aacb 100644
--- a/src/helper/response-handler.js
+++ b/src/helper/response-handler.js
@@ -40,7 +40,7 @@ function wrapCallback(cb) {
       return cb(errObj);
     }
 
-    return cb(null, (data.type && data.type === 'text/html') ? data.text : objectHelper.toCamelCase(data.body || data));
+    return cb(null, ((data.type && data.type === 'text/html') ? data.text : objectHelper.toCamelCase(data.body || data)));
   };
 }
 


Test patch:
diff --git a/test/authentication/authentication.test.js b/test/authentication/authentication.test.js
index bd94398..37b2d4e 100644
--- a/test/authentication/authentication.test.js
+++ b/test/authentication/authentication.test.js
@@ -405,6 +405,49 @@ describe('auth0.authentication', function () {
 
   });
 
+  context('getUserCountry', function () {
+    before(function() {
+      this.auth0 = new Authentication({
+        domain: 'me.auth0.com',
+        clientID: '...',
+        redirectUri: 'http://page.com/callback',
+        responseType: 'code',
+        _sendTelemetry: false
+      });
+    });
+
+    afterEach(function(){
+      request.get.restore();
+    })
+
+    it('should return the user country code', function(done) {
+      stub(request, 'get', function(url) {
+        expect(url).to.be('https://me.auth0.com/user/geoloc/country')
+        return new RequestMock({
+          headers: {
+            'Content-Type': 'application/json'
+          },
+          cb: function(cb) {
+            cb(null, {
+              body: {
+                'country_code': 'AR'
+              }
+            });
+          }
+        });
+      });
+
+      this.auth0.getUserCountry(function(err, data) {
+        expect(err).to.be(null);
+        expect(data).to.eql({
+          countryCode: 'AR'
+        });
+        done();
+      })
+    });
+
+  });
+
   context('getSSOData', function () {
     before(function() {
       this.auth0 = new Authentication({
diff --git a/test/helper/response-handler.test.js b/test/helper/response-handler.test.js
index 4e68b50..6e9f1a5 100644
--- a/test/helper/response-handler.test.js
+++ b/test/helper/response-handler.test.js
@@ -135,7 +135,8 @@ describe('helpers responseHandler', function () {
 
   it('should return the data 2', function (done) {
     var assert_data = {
-      text: 'The reponse message'
+      text: 'The reponse message',
+      type: 'text/html'
     }
 
     responseHandler(function(err, data) {
diff --git a/test/web-auth/redirect.test.js b/test/web-auth/redirect.test.js
index a87ad46..1d5f483 100644
--- a/test/web-auth/redirect.test.js
+++ b/test/web-auth/redirect.test.js
@@ -104,7 +104,8 @@ describe('auth0.WebAuth.redirect', function () {
           },
           cb: function (cb) {
             cb(null, {
-              text: 'the_form_html'
+              text: 'the_form_html',
+              type: 'text/html'
             });
           }
         });