question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RTKQuery SSR custom auth headers.

See original GitHub issue

Hello 😃

I’m trying to work out the best approach to using RTKQuery with SSR and auth cookies/headers.

At the moment we are using cookies for our auth, which means that in a browser environment (client side) every fetch request automatically attaches the cookies and auth succeeds.

On the server side, this is a little more tricky. We can access the cookies in next.js via the server side context prop. (eg ctx.req.headers), but then I’m not sure how this cookie header can be passed into rtkquery.

I can think of one option:

We can insert cookie data into the redux store on the server side and then in the prepareHeaders function we can access the store with getState() and set a cookie header - This way we only have the auth logic in one place, but I am weary about inserting cookie creds into the store. I guess I could dispatch an action afterwards to remove the cookies so they aren’t hydrated to the client side… but it sounds messy.

Is there a more established way of achieving this or does this sound right?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
phryneascommented, Apr 11, 2022

Yup, that’s my suggestion. extra will not be hydrated, only the state.

2reactions
joel1stcommented, Apr 11, 2022

Hmm. Can’t you inject the current request into the store thunk’s extra when you create the store in same way? That would be how I’d go about it 🤔

next-redux-wrapper’s makeStore should have access to Context.

Thanks for the reply @phryneas - this approach sounds really promising. Just to confirm, are you suggesting to add to the extraArgument via genDefaultMiddleware https://redux-toolkit.js.org/api/getDefaultMiddleware and then consume that extra inside prepareHeaders? That sounds like it will work well (assuming extra is not hydrated to the client side).

Read more comments on GitHub >

github_iconTop Results From Across the Web

fetchBaseQuery
A callback that receives a Headers object and determines the body field of the FetchArgs argument should be stringified via JSON.stringify() .
Read more >
How can i get Authorization key from header in RTK Query
Is there any chance to access the 'Authorization' from the response header. I try it to build an custom baseQuery but i failed....
Read more >
Creating React Apps With Redux Toolkit and RTK Query
We use our custom base query here, meaning that every request in the scope of userApi will include an Authorization header.
Read more >
fetchBaseQuery
Setting default headers on requests# ... The most common use case for prepareHeaders would be to automatically include authorization headers for your API...
Read more >
RTK-Query Authorization : r/reduxjs
Anybody used RTKQ for auth purposes? Successfully got signup and login working. But can't figure out how to save the token that is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found