Skip to content

Commit

Permalink
Update functions to use async/await model
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcsz committed Mar 1, 2017
1 parent 83d19ef commit 8ef561f
Showing 1 changed file with 20 additions and 23 deletions.
43 changes: 20 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,38 @@
const fetch = require('node-fetch');
const encode = require('./encode.js');

exports.formAuthorizationUri = (config) => {
return new Promise ((fulfill, reject) => {
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.formAuthorizationUri = async function (config) {
if (config.client.id === undefined || config.client.secret === undefined || config.uri.authorization === undefined || config.uri.redirect === undefined || config.scope === undefined) {
return 'Not enough information to form authorization URL';
} else {
if (config.responseType !== undefined) {
return `${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) {
return `${config.uri.authorization}?client_id=${encodeURI(config.client.id)}&scope=${encodeURI(config.scope)}&redirect_uri=${encodeURIComponent(config.uri.redirect)}`;
}
});
}
}

exports.getAccessToken = (config) => {
return new Promise ((fulfill, reject) => {
let encodedClientCredentials = encode.base64(`${config.client.id}:${config.client.secret}`);
exports.getAccessToken = async function (config) {
let accessToken;

fetch(config.uri.token, {
try {
accessToken = await fetch(config.uri.token, {
method: 'POST',
headers: {
'Authorization': `Basic ${encodedClientCredentials}`,
'Content-Type': 'application/x-www-form-urlencoded'
},
body: `code=${encodeURI(config.code)}&grant_type=authorization_code&redirect_uri=${encodeURI(config.uri.redirect)}`
}).catch((error) => {
let tokenError = error.json();
reject(tokenError);
}).then((response) => {
return response.json().catch((parseError) => {
console.log(`Error parsing token: ${parseError}`);
reject(parseError);
console.log(`Error parsing token response: ${parseError}`);
});
}).then((accessToken) => {
fulfill(accessToken);
});
});
} catch (error) {
console.log(`Error fetching token: ${error}`);
return error;
}

return accessToken;
}

0 comments on commit 8ef561f

Please sign in to comment.