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.

prepare headers getState token for server side request

See original GitHub issue

I’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:closed
  • Created a year ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
phryneascommented, Mar 27, 2022

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.

0reactions
ghostmurdacommented, Nov 9, 2022

@bryantobing12 Would you be interested in sharing how and maybe what libraries you used. Next auth already writes the jwt to the cookies but im not sure how to access it on the server. Since prepareHeaders doesn’t have access to the request

@d-sooter #2228 Here you can find the answer. urhot/nextjs-rtk-query-ssr@54d47f9 In this repo you can find the example to access cookies in prepareHeaders with next-redux-wrapper

Thanks! Great solution

Read more comments on GitHub >

github_iconTop 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 >

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