From 1688091d034fbe5d4ddb7c8c86020e61b6b76249 Mon Sep 17 00:00:00 2001 From: David Sanchez Date: Sat, 11 Feb 2017 20:35:10 -0800 Subject: [PATCH] Rewrite to fix fetch issues --- index.js | 49 +++++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/index.js b/index.js index 96e8b11..53bfd60 100644 --- a/index.js +++ b/index.js @@ -1,40 +1,41 @@ const fetch = require('node-fetch'); -const encode = require('./lib/encode.js'); +const encode = require('./encode.js'); -exports.authorizationCodeUri = (appCredentials) => { +exports.formAuthorizationUri = (config) => { return new Promise ((fulfill, reject) => { - if (appCredentials.client.id === undefined) { - reject('Not enough information to build authorization URL.'); - } - if (appCredentials.responseType !== undefined) { - fulfill(`${appCredentials.uri.authorization}?client_id=${encodeURI(appCredentials.client.id)}&response_type=${encodeURI(appCredentials.responseType)}&redirect_uri=${encodeURI(appCredentials.uri.redirect)}&scope=${appCredentials.scope}`); - } else if (appCredentials.responseType === undefined) { - fulfill(`${appCredentials.uri.authorization}?client_id=${encodeURI(appCredentials.client.id)}&redirect_uri=${encodeURI(appCredentials.uri.redirect)}&scope=${appCredentials.scope}`); + if (config.client.id === undefined || config.client.secret === undefined || config.uri.authorization === undefined || config.uri.redirect === undefined || config.scope === undefined) { + reject('Not enough information to form authorization URL'); + } else { + if (config.responseType !== undefined) { + fulfill(`${config.uri.authorization}?client_id=${encodeURI(config.client.id)}&response_type=${encodeURI(config.responseType)}&scope=${encodeURI(config.scope)}&redirect_uri=${encodeURIComponent(config.uri.redirect)}`); + } else if (config.responseType === undefined) { + fulfill(`${config.uri.authorization}?client_id=${encodeURI(config.client.id)}&scope=${encodeURI(config.scope)}&redirect_uri=${encodeURIComponent(config.uri.redirect)}`); + } } }); } -exports.getAccessToken = (app) => { +exports.getAccessToken = (config) => { return new Promise ((fulfill, reject) => { - let encodedClientCredentials = encode.base64Encode(`${app.client.id}:${app.client.secret}`); + let encodedClientCredentials = encode.base64(`${config.client.id}:${config.client.secret}`); - fetch(app.uri.authorization, { + fetch(config.uri.token, { method: 'POST', - header: { + headers: { 'Authorization': `Basic ${encodedClientCredentials}`, - 'Content-Type': 'application/x-ww-form-urlencoded' + 'Content-Type': 'application/x-www-form-urlencoded' }, - body: `client_id=${app.client.id}&grant_type=authorization_code&redirect_uri-&${encodeURI(app.uri.redirect)}&code=${app.code}` - }).then((token) => { - let accessToken = token.json(); - - if (accessToken.error !== undefined) { - reject(accessToken); - } else { - fulfill(accessToken); - } + body: `code=${encodeURI(config.code)}&grant_type=authorization_code&redirect_uri=${encodeURI(config.uri.redirect)}` }).catch((error) => { - reject(error); + let tokenError = error.json(); + reject(tokenError); + }).then((response) => { + return response.json().catch((parseError) => { + console.log(`Error parsing token: ${parseError}`); + reject(parseError); + }); + }).then((accessToken) => { + fulfill(accessToken); }); }); } \ No newline at end of file