Fixed several inheritance bugs in Usergrid>UsergridClient, appAuth now instantiates automatically if clientId and secret exist on Usergrid.init()
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 @@
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.listen(process.env.port || 9000)
+// app.listen(process.env.port || 9000)
/*
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 @@
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
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 @@
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 @@
}
})
- 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 @@
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()')
diff --git a/usergrid.js b/usergrid.js
index 765378f..cc6ba27 100644
--- a/usergrid.js
+++ b/usergrid.js
@@ -14,17 +14,20 @@
'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
}