Repository: auth0/node-auth0
Pull request #28 (2015-04-10 17:39:51)

Support for generic domains


Patch:
diff --git a/README.md b/README.md
index 1cffb2892..fd1d74864 100644
--- a/README.md
+++ b/README.md
@@ -26,6 +26,15 @@ var auth0 = require('auth0')({
 });
 ~~~
 
+Alternatively you can just set the domain:
+~~~js
+var token = '{YOUR_API_V2_TOKEN}';
+var auth0 = require('auth0')({
+  token: token,
+  domain: 'login.eu.auth0.com'
+});
+~~~
+
 ### User's metadata
 You can store data about a user that does not come from the user's identity provider. This is known as "metadata". There are two types of metadata: `user_metadata` and `app_metadata`. You can find out more [here](https://auth0.com/docs/apiv2Changes#8).
 
diff --git a/package.json b/package.json
index 75ea3023a..86fca7112 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "auth0",
-  "version": "2.0.0-alpha.1",
+  "version": "2.0.0-alpha.2",
   "description": "node.js SDK for Auth0 API v2",
   "main": "index.js",
   "scripts": {
diff --git a/src/client.js b/src/client.js
index be1e4b413..0e173a8e7 100644
--- a/src/client.js
+++ b/src/client.js
@@ -14,7 +14,12 @@ function Client(options){
     throw new ArgumentError('Missing token');
   }
 
-  var domain = REGIONS_TO_DOMAINS[options.region || DEFAULT_REGION];
+  if (options.domain && options.region){
+    throw new ArgumentError('Cannot provide both region and domain');
+  }
+
+  var domain = options.domain ||
+    REGIONS_TO_DOMAINS[options.region || DEFAULT_REGION];
 
   if (!domain){
     throw new ArgumentError('The region is invalid, valid values are any of: "' +


Test patch:
diff --git a/test/domains.tests.js b/test/domains.tests.js
new file mode 100644
index 000000000..5ebb3784e
--- /dev/null
+++ b/test/domains.tests.js
@@ -0,0 +1,99 @@
+var expect = require('chai').expect;
+var nock = require('nock');
+var util = require('util');
+var constants = require('./constants');
+
+var TOKEN = 'token';
+
+
+describe('regions', function(){
+  var user_id = 'google-oauth%7C1234';
+  var url = util.format(constants.USER_SUB_ROUTE, user_id);
+  var update = {
+    roles: ['reader', 'writer'],
+    permissions: null
+  };
+
+  var body = {
+    app_metadata: update
+  };
+
+  var localNock, auth0;
+
+  before(function(){
+    auth0 = require('..')({
+      token: TOKEN,
+      region: 'eu'
+    });
+
+    localNock = nock(constants.EU_BASE_API_URL, {
+      reqheaders: {
+        'Authorization': 'Bearer ' + TOKEN
+      }
+    })
+    .patch(url, body)
+    .reply(401, {
+      'statusCode': 401,
+      'error': 'Unauthorized',
+      'message': 'Invalid token'
+    });
+  });
+
+  after(nock.cleanAll);
+
+  it ('should perform request against correct domain', function(){
+    return auth0.user(user_id).appMetadata.update(update).catch(function(err){
+      expect(err).to.not.be.undefined;
+      expect(err.statusCode).to.equal(401);
+      expect(err.error).to.equal('Unauthorized');
+      expect(err.message).to.equal('Invalid token');
+      localNock.done();
+    });
+  });
+});
+
+describe('domains', function(){
+  var user_id = 'google-oauth%7C1234';
+  var url = util.format(constants.USER_SUB_ROUTE, user_id);
+  var update = {
+    roles: ['reader', 'writer'],
+    permissions: null
+  };
+
+  var body = {
+    app_metadata: update
+  };
+
+  var localNock, auth0;
+
+  before(function(){
+    auth0 = require('..')({
+      token: TOKEN,
+      domain: 'login0.myauth0.com'
+    });
+
+    localNock = nock('https://login0.myauth0.com/api/v2', {
+      reqheaders: {
+        'Authorization': 'Bearer ' + TOKEN
+      }
+    })
+    .patch(url, body)
+    .reply(401, {
+      'statusCode': 401,
+      'error': 'Unauthorized',
+      'message': 'Invalid token'
+    });
+  });
+
+  after(nock.cleanAll);
+
+  it ('should perform request against correct domain', function(){
+    return auth0.user(user_id).appMetadata.update(update).catch(function(err){
+      expect(err).to.not.be.undefined;
+      expect(err.statusCode).to.equal(401);
+      expect(err.error).to.equal('Unauthorized');
+      expect(err.message).to.equal('Invalid token');
+      localNock.done();
+    });
+  });
+});
\ No newline at end of file
diff --git a/test/errors.tests.js b/test/errors.tests.js
index 0202ea814..eeae4f8a5 100644
--- a/test/errors.tests.js
+++ b/test/errors.tests.js
@@ -13,4 +13,9 @@ describe('errors', function(){
     expect(Auth0.bind(null, { token: '' }))
       .to.throw(ArgumentError, 'Missing token');
   });
+
+  it('should not allow domain and region', function(){
+    expect(Auth0.bind(null, { token: 'token', region: 'sa', domain: 'login.sa.auth0.com' }))
+      .to.throw(ArgumentError, 'Cannot provide both region and domain');
+  });
 });
\ No newline at end of file
diff --git a/test/regions.tests.js b/test/regions.tests.js
deleted file mode 100644
index bd4462389..000000000
--- a/test/regions.tests.js
+++ /dev/null
@@ -1,51 +0,0 @@
-var expect = require('chai').expect;
-var nock = require('nock');
-var util = require('util');
-var constants = require('./constants');
-
-var TOKEN = 'token';
-var auth0 = require('..')({
-  token: TOKEN,
-  region: 'eu'
-});
-
-describe('regions', function(){
-  var user_id = 'google-oauth%7C1234';
-  var url = util.format(constants.USER_SUB_ROUTE, user_id);
-  var update = {
-    roles: ['reader', 'writer'],
-    permissions: null
-  };
-
-  var body = {
-    app_metadata: update
-  };
-
-  var localNock;
-
-  before(function(){
-    localNock = nock(constants.EU_BASE_API_URL, {
-      reqheaders: {
-        'Authorization': 'Bearer ' + TOKEN
-      }
-    })
-    .patch(url, body)
-    .reply(401, {
-      'statusCode': 401,
-      'error': 'Unauthorized',
-      'message': 'Invalid token'
-    });
-  });
-
-  after(nock.cleanAll);
-
-  it ('should perform request against correct domain', function(){
-    return auth0.user(user_id).appMetadata.update(update).catch(function(err){
-      expect(err).to.not.be.undefined;
-      expect(err.statusCode).to.equal(401);
-      expect(err.error).to.equal('Unauthorized');
-      expect(err.message).to.equal('Invalid token');
-      localNock.done();
-    });
-  });
-});
\ No newline at end of file