Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: send token in Authorization header #102

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const config = require('./config');
const form = require('form-urlencoded');
const Promise = require('es6-promise').Promise;

const sendRequest = (method, url, data, progress) => {
const sendRequest = (method, url, data, progress, token) => {
let xhr;
const requestPromise = new Promise((resolve) => {
const isFormData = global.FormData && (data instanceof FormData);
Expand All @@ -17,6 +17,10 @@ const sendRequest = (method, url, data, progress) => {
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
}

if (token) {
xhr.setRequestHeader('Authorization', `OAuth ${token}`);
}

xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
resolve({responseText: xhr.responseText, request: xhr});
Expand Down Expand Up @@ -72,13 +76,13 @@ const parseResponse = ({responseText, request}) => {
* @param {Function=} progress upload progress handler
* @return {Promise}
*/
const sendAndFollow = (method, url, data, progress) => {
const requestPromise = sendRequest(method, url, data, progress);
const sendAndFollow = (method, url, data, progress, token) => {
const requestPromise = sendRequest(method, url, data, progress, token);
const followPromise = requestPromise.then(({responseText, request}) => {
const response = parseResponse({responseText, request});

if (response.json && response.json.status === '302 - Found') {
return sendAndFollow('GET', response.json.location, null);
return sendAndFollow('GET', response.json.location, null, token);
} else {
if (request.status !== 200 && response.error) {
throw response.error;
Expand Down Expand Up @@ -119,10 +123,8 @@ module.exports = {

additionalParams.format = 'json';

// set the oauth_token or, in case none has been issued yet, the client_id
if (oauthToken) {
additionalParams.oauth_token = oauthToken;
} else {
// in case not token has been issued yet, set the client_id
if (!oauthToken) {
additionalParams.client_id = clientId;
}

Expand All @@ -141,7 +143,7 @@ module.exports = {
// construct request url
url = `${config.get('baseURL')}${path}?${form.encode(params)}`;

return sendAndFollow(method, url, data, progress);
return sendAndFollow(method, url, data, progress, oauthToken);
},

/**
Expand Down