0
export const getServerSideProps = wrapper.getServerSideProps(async (ctx) => {
    const access_token = cookies(ctx).access_token;
    await ctx.store.dispatch(getData(access_token));
    if (localStorage){
        localStorage.setItem("token", "1234");
        console.log(localStorage.getItem("token"));
    }

});

localStorage is not defined on this line if (localStorage){}

  • 1
    localStorage is actually window.localStorage. window will be always undefined on server side. You can use `if( process.browser ) { ... }` to execute code only on client side. Read more here.. https://stackoverflow.com/questions/55151041/window-is-not-defined-in-next-js-react-app – Jimish Fotariya Dec 30 '20 at 10:46

2 Answers2

0

you need to declare it somewhere, like in the top of the js file, specifying it is global:

/* global localStorage, */

or, alternatively, use the version tied to window (it is your global object)

window.localStorage.setItem("token", "1234");
Nooruddin Lakhani
  • 7,507
  • 2
  • 19
  • 39
0

Store your token as Cookie instead of LocalStorage.

document.cookie = "username=John Doe; expires=Thu, 30 Jan 2021 12:00:00 UTC; path=/";

and access inside getInitialProps or Server Side Methods from "req.headers.cookie"

Prabhu
  • 688
  • 5
  • 11