Skip to content

Commit

Permalink
Merge pull request #267 from auth0/v8-usercountry
Browse files Browse the repository at this point in the history
Add get user country method for passwordless
  • Loading branch information
hzalaz authored Dec 14, 2016
2 parents d41efff + 935a1e7 commit 32eefad
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 3 deletions.
18 changes: 18 additions & 0 deletions src/authentication/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 1 addition & 1 deletion src/helper/response-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
};
}

Expand Down
43 changes: 43 additions & 0 deletions test/authentication/authentication.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
3 changes: 2 additions & 1 deletion test/helper/response-handler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion test/web-auth/redirect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
});
}
});
Expand Down

0 comments on commit 32eefad

Please sign in to comment.