Reduced cyclomatic complexity of authenticateApp()
diff --git a/lib/client.js b/lib/client.js
index c452a81..3328634 100644
--- a/lib/client.js
+++ b/lib/client.js
@@ -101,10 +101,13 @@
UsergridClient.prototype.authenticateApp = function(options, callback) {
var self = this
callback = helpers.cb(callback || options)
+
+ options.clientId = options.clientId || self.appAuth.clientId
+ options.clientSecret = options.clientSecret || self.appAuth.clientSecret
+
if (!(self.appAuth instanceof UsergridAppAuth)) {
throw new Error('App auth context was not defined when attempting to call .authenticateApp()')
- }
- if (!(options.clientId || self.appAuth.clientId) && !(options.clientSecret || self.appAuth.clientSecret)) {
+ } else if (!options.clientId || !options.clientSecret) {
throw new Error('authenticateApp() failed because clientId or clientSecret are missing')
}
options.type = 'token'
@@ -114,17 +117,16 @@
headers: helpers.userAgent,
body: {
grant_type: 'client_credentials',
- client_id: options.clientId || self.appAuth.clientId,
- client_secret: options.clientSecret || self.appAuth.clientSecret
+ client_id: options.clientId,
+ client_secret: options.clientSecret
},
method: 'POST',
json: true
}, function(error, response, body) {
- var token = body.access_token || undefined
- self.appAuth.token = token
+ self.appAuth.token = body.access_token
self.appAuth.expiry = Date.now() + ((body.expires_in ? body.expires_in - 5 : 0) * 1000)
self.appAuth.tokenTtl = body.expires_in
- callback(error, response, token)
+ callback(error, response, body.access_token)
})
}