Fixed several inheritance bugs in Usergrid>UsergridClient, appAuth now instantiates automatically if clientId and secret exist on Usergrid.init()
Project: http://git-wip-us.apache.org/repos/asf/usergrid-nodejs/repo Commit: http://git-wip-us.apache.org/repos/asf/usergrid-nodejs/commit/0bfa2a25 Tree: http://git-wip-us.apache.org/repos/asf/usergrid-nodejs/tree/0bfa2a25 Diff: http://git-wip-us.apache.org/repos/asf/usergrid-nodejs/diff/0bfa2a25 Branch: refs/heads/master Commit: 0bfa2a250f30e34bd7cb9bda4c002e040b089976 Parents: fca2b08 Author: brandon <bran...@behemoth.ca> Authored: Thu Feb 18 14:08:05 2016 -0800 Committer: brandon <bran...@behemoth.ca> Committed: Thu Feb 18 14:08:05 2016 -0800 ---------------------------------------------------------------------- examples/api-proxy/app.js | 5 +++-- lib/appAuth.js | 13 ++++++++----- lib/client.js | 29 ++++++++++++++++++++++------- usergrid.js | 7 +++++-- 4 files changed, 38 insertions(+), 16 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/usergrid-nodejs/blob/0bfa2a25/examples/api-proxy/app.js ---------------------------------------------------------------------- diff --git a/examples/api-proxy/app.js b/examples/api-proxy/app.js index feed0ac..76b3401 100644 --- a/examples/api-proxy/app.js +++ b/examples/api-proxy/app.js @@ -6,7 +6,8 @@ var express = require('express'), Usergrid.init() -console.log(Usergrid.appAuth) +// Usergrid.setAppAuth(id, secret) +// console.log(Usergrid.appAuth) Usergrid.authenticateApp(function(err, usergridResponse) { if (usergridResponse.ok) { console.log('app is now authenticated') @@ -19,7 +20,7 @@ app.get('/:collection/:uuidOrName?', function(req, res) { }) }) -app.listen(process.env.port || 9000) +// app.listen(process.env.port || 9000) /* http://git-wip-us.apache.org/repos/asf/usergrid-nodejs/blob/0bfa2a25/lib/appAuth.js ---------------------------------------------------------------------- diff --git a/lib/appAuth.js b/lib/appAuth.js index d1a32ef..740002e 100644 --- a/lib/appAuth.js +++ b/lib/appAuth.js @@ -19,15 +19,18 @@ var UsergridAuth = require('./auth'), util = require('util'), _ = require('lodash') -var UsergridAppAuth = function(options) { +var UsergridAppAuth = function() { var self = this var args = _.flattenDeep(helpers.args(arguments)) if (_.isPlainObject(args[0])) { - options = args[0] + self.clientId = args[0].clientId + self.clientSecret = args[0].clientSecret + self.tokenTtl = args[0].tokenTtl + } else { + self.clientId = args[0] + self.clientSecret = args[1] + self.tokenTtl = args[2] } - self.clientId = options.clientId || args[0] - self.clientSecret = options.clientSecret || args[1] - self.tokenTtl = options.tokenTtl || args[2] UsergridAuth.call(self) _.assign(self, UsergridAuth) return self http://git-wip-us.apache.org/repos/asf/usergrid-nodejs/blob/0bfa2a25/lib/client.js ---------------------------------------------------------------------- diff --git a/lib/client.js b/lib/client.js index dadf4b8..f022793 100644 --- a/lib/client.js +++ b/lib/client.js @@ -46,6 +46,18 @@ var UsergridClient = function(options) { throw new Error('"orgId" and "appId" parameters are required when instantiating UsergridClient') } + Object.defineProperty(self, 'test', { + enumerable: false + }) + + Object.defineProperty(self, 'clientId', { + enumerable: false + }) + + Object.defineProperty(self, 'clientSecret', { + enumerable: false + }) + Object.defineProperty(self, 'appAuth', { get: function() { return __appAuth @@ -59,10 +71,10 @@ var UsergridClient = function(options) { } }) - Object.defineProperty(self, 'test', { - enumerable: false - }) - + // if client ID and secret are defined on initialization, initialize appAuth + if (self.clientId && self.clientSecret) { + self.setAppAuth(self.clientId, self.clientSecret) + } return self } @@ -88,13 +100,16 @@ UsergridClient.prototype = { getConnections: function() { return new UsergridRequest(helpers.build.getConnections(this, helpers.args(arguments))) }, - setAppAuth: function(options) { - this.appAuth = (_.isString(options)) ? helpers.args(arguments) : options + setAppAuth: function() { + this.appAuth = new UsergridAppAuth(helpers.args(arguments)) }, authenticateApp: function(options) { var self = this var callback = helpers.cb(helpers.args(arguments)) - var auth = (options instanceof UsergridAppAuth) ? options : self.appAuth || new UsergridAppAuth(options) + console.log(self.appAuth)//, self.appAuth, new UsergridAppAuth(options), new UsergridAppAuth(self.clientId, self.clientSecret)) + var auth = _.first([options, self.appAuth, new UsergridAppAuth(options), new UsergridAppAuth(self.clientId, self.clientSecret)].filter(function(p) { + return p instanceof UsergridAppAuth + })) if (!(auth instanceof UsergridAppAuth)) { throw new Error('App auth context was not defined when attempting to call .authenticateApp()') http://git-wip-us.apache.org/repos/asf/usergrid-nodejs/blob/0bfa2a25/usergrid.js ---------------------------------------------------------------------- diff --git a/usergrid.js b/usergrid.js index 765378f..cc6ba27 100644 --- a/usergrid.js +++ b/usergrid.js @@ -14,17 +14,20 @@ limitations under the License. 'use strict' +var _ = require('lodash') + var Usergrid = { isInitialized: false, isSharedInstance: true, initSharedInstance: function(options) { var self = this if (self.isInitialized) { + console.log('Usergrid shared instance is already initialized') return self } var UsergridClient = require('./lib/client') - Object.setPrototypeOf(Usergrid, new UsergridClient(options)) - UsergridClient.call(self) + Object.setPrototypeOf(self, new UsergridClient(options)) + _.assign(self, new UsergridClient(options)) self.isInitialized = true self.isSharedInstance = true }