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

Support For Authorization Headers #3

Closed
adamsanderson opened this issue Feb 15, 2017 · 10 comments
Closed

Support For Authorization Headers #3

adamsanderson opened this issue Feb 15, 2017 · 10 comments

Comments

@adamsanderson
Copy link
Contributor

Is there a way to set headers requests?

It's common to use a header like Authorization: bearer random-token for authenticating against a restful API. I saw that there's an options hash that can be passed along to the middleware, but those only seem to be used for the HTTP method.

If the middleware supported a headers option, or something similar, you could intercept REQUEST_ASYNC actions with an authorization middleware before it hit middleware/query.js and merge in any headers your application needs.

Is that a reasonable direction for a pull request? Is there a better solution?

@ryanashcraft
Copy link
Contributor

@adamsanderson Sounds great! I think it makes most sense to put it in options (similar to request and fetch APIs). Would love to see a PR for this!

@adamsanderson
Copy link
Contributor Author

Sounds good. I'll give it a shot this evening.

@adamsanderson
Copy link
Contributor Author

Kind of embarrassing, but I'm stuck...

I'm pretty sure the change just boils down to adding this near where the body is being set:

if (body) {
    request.send(body);
}

if (options.headers) {
    request.set(options.headers);
}

I wanted to test that with the example app, but following the directions on the readme leaves me with a build error when I run npm start:

$ cd examples/async/
$ npm install
$ npm start

npm run start

> [email protected] start /Users/adam.sanderson/Repos/redux-query/examples/async
> node server.js

==> 🌎  Listening on port 3000. Open up http://localhost:3000/ in your browser.
webpack built 2018d9a5db28c107f28b in 2983ms
Hash: 2018d9a5db28c107f28b
Version: webpack 1.14.0
Time: 2983ms
        Asset     Size  Chunks       Chunk Names
    bundle.js  1.35 MB       0       main
bundle.js.map  1.61 MB       0       main
chunk    {0} bundle.js, bundle.js.map (main) 1.25 MB [rendered]
    [0] multi main 40 bytes {0} [built]

...

  [667] (webpack)-hot-middleware/process-update.js 3.88 kB {0} [built]

ERROR in ./containers/App.js
Module not found: Error: Cannot resolve module 'redux-query' in /Users/adam.sanderson/Repos/redux-query/examples/async/containers
 @ ./containers/App.js 31:18-40

ERROR in ./store/configureStore.js
Module not found: Error: Cannot resolve module 'redux-query' in /Users/adam.sanderson/Repos/redux-query/examples/async/store
 @ ./store/configureStore.js 22:18-40

ERROR in ./reducers/index.js
Module not found: Error: Cannot resolve module 'redux-query' in /Users/adam.sanderson/Repos/redux-query/examples/async/reducers
 @ ./reducers/index.js 11:18-40

Any suggestions? Sorry if this is a bit remedial, but wrangling webpack and npm doesn't appear to be my strong suit.

@ryanashcraft
Copy link
Contributor

@adamsanderson No worries! Did you run npm run build (from the root directory) before npm installing/running the async example? (I should add this to the README.)

@adamsanderson
Copy link
Contributor Author

@ryanashcraft that explains everything! I was able to get it running at one point, but changes weren't being reflected. Now I realize that I did try building it. Ok, I think I'm unblocked, I'll try that out tonight.

@peterlandry
Copy link

Is there a workaround until this PR lands?

@adamsanderson
Copy link
Contributor Author

I don't think so, nothing outside of the middleware has a chance to touch the superagent request.

@peterlandry
Copy link

Ahh. That's too bad.

@ryanashcraft
Copy link
Contributor

Resolved by #10. Will go out as part of 1.1.0 but in meantime is available by installing redux-query@next.

@samuelcastro
Copy link

samuelcastro commented Nov 14, 2018

Sorry but it's still not clear for me how I can automatically inject the authorization header (jwt) in all ongoing requests through a middleware.

Any idea?

mili-confluent pushed a commit to mili-confluent/redux-query that referenced this issue Jan 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants