trelliscope / trelliscopejs-lib

JavaScript viewer for Trelliscope displays
BSD 3-Clause "New" or "Revised" License
29 stars 7 forks source link

Add Authorization header to JSON/JSONP requests? #10

Open aeksco opened 7 years ago

aeksco commented 7 years ago

Hi!

I'm working with the DJ team at CASE and we're in the process of integrating TrelliscopeJS as one of our visualization options. Our backend is currently configured to serve the requisite static files behind an authentication wall - I wanted to know it there's a way to configure the getJSON and getJSONP functions located in src/actions/index.js to pass an Authorization header along with the request.

If this functionality doesn't currently exist I would be more than happy to help implement the feature - an additional Redux action named GET_REQUEST_HEADERS with a corresponding callback defined in the options object passed into the trelliscopeApp function would probably do the trick, though admittedly my knowledge of the codebase is cursory at best.

I was thinking something like the following:


let options = {
  callbacks: {
    "GET_REQUEST_HEADERS": (e) => { return { "Authorization": localStorage.token }; }
  }
};

this.trelliscope = trelliscopeApp(trelliscope_id, path, options);

Let me know if I can lend a hand!

-A

hafen commented 7 years ago

This would be awesome functionality to have! I'm not an expert on http requests with authentication, so I could benefit from your help here.

The package I'm using to get the jsonp is this one: https://github.com/larryosborn/JSONP/blob/master/lib/jsonp.js. I also use d3-request (https://github.com/d3/d3-request) in the case of plain json. If you can find a way to make authentication headers work with either one of those in a general sense I can help guide you through how this app works so we can get it plugged in. The app provides the choice of either jsonp or json (jsonp so that people can run apps without a web server), so as long as we can get one working (ideally both), we should be in good shape.

aeksco commented 7 years ago

@hafen Thanks for the prompt reply! I'll take a peek at the documentation for those two libraries - hopefully something as simple as adding a header won't be an agonizing endeavor :)

I'll touch base shortly with an update on my findings - chat soon!