prepare headers getState token for server side request
See original GitHub issueI’m currently following this example in order to authenticate users when sending requests to the API. https://redux-toolkit.js.org/rtk-query/api/fetchBaseQuery#setting-default-headers-on-requests
import { fetchBaseQuery } from '@reduxjs/toolkit/query'
import type { RootState } from './store'
const baseQuery = fetchBaseQuery({
baseUrl: '/',
prepareHeaders: (headers, { getState }) => {
const token = (getState() as RootState).auth.token
// If we have a token set in state, let's assume that we should be passing it.
if (token) {
headers.set('authorization', `Bearer ${token}`)
}
return headers
},
})
but the problem is, the token will always be empty on the server-side part. causing the request made will always client-side request. how do I resolve this issue?
I’m using nextjs, and i store the token using redux-persist. and i’m following this https://redux-toolkit.js.org/rtk-query/usage/server-side-rendering to setup hydration on nextjs. if the api
doesn’t need token the server side request works fine.
Issue Analytics
- State:
- Created a year ago
- Comments:10 (3 by maintainers)
Top Results From Across the Web
fetchBaseQuery - Redux Toolkit
This is a very small wrapper around fetch that aims to simplify HTTP requests. It is not a full-blown replacement for axios ,...
Read more >How to make sure the axios request is using the latest token ...
I have an accessToken in my redux store. I am creating a axios client like this: import axios from 'axios'; import store from...
Read more >Handling user authentication with Redux Toolkit
In this article, we'll learn how to use Redux Toolkit (RTK) to create a frontend authentication workflow in React.
Read more >Obtaining an Access Token - OAuth 2.0 Simplified
Here we are sending a request to GitHub's token endpoint to exchange the authorization code for an access token. The request contains our...
Read more >NextJS SSR - JWT (Access/Refresh Token) Authentication ...
The client wants to make a request but it gets rejected with 401 status ... refresh token Node.js server path undefined, { headers,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Well, the Redux store is always empty on the server as well - SSR always uses a “new” Redux store since technically there is no way the client could communicate the full store contents back to the server witch each request.
You can move data from the server store to the client store with each request (using the hydration options redux-next-wrapper offers), but never the other way round.
If you want something like a token shared between client and server, you would have to put that into a cookie and in
prepareHeaders
access that cookie, not into your Redux store.Thanks! Great solution