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.

How to use getInitialProps to get cookies?

See original GitHub issue

I’m facing an issue with next.js

I cannot get my cookies when i make a request from async static getInitialProps. i get undefined

However when i am making it in componentWillMount there is no problem. Unfortunately, it’s too late because i need to get the cookie info before the component be called. So i need to get it in getInitialProps

Here what i’ve already tried without success :

static async getInitialProps () { const res = await axios.get('http://mybackend/getCookie'); return {data : res.data} } //res.data = undefined

the purpose is to check the user authentication status in firebase in order to render the appropriate page

Any suggestion ? thanks

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
jackmatrixcommented, Jun 26, 2017

I could finally solve this issue. the problem was inside the getinitialprops rather than making req.cookies, i had to do req.headers.cookie and all are working just as expected. Thanks for help.

2reactions
davidnguyen11commented, Jun 26, 2017

you can use cookie-parser package. Use it in server.js as follows:

app.prepare().then(() => {
  const server = express();
  server.use(cookieParser());
});

index.js

  static async getInitialProps(context) {
    const { store, isServer, query, req } = context;
    if (isServer) {
      if (isServer) {
         console.log(req.cookies);
      }
    }
    return { isServer };
  }
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use method getInitialProps from next.js to get cookies?
I'm using this method to be able to grab some initial data in getInitialProps while rendering server-side, but are there any security ...
Read more >
Next.js: How to get and set cookies - Max Schmitt
In a Next.js app, you might find yourself wanting to access cookies during getInitialProps , getServerSideProps or within an API route.
Read more >
Next.js — Handling Cookies in getServerSideProps
I use next-cookies to get cookies on the server: import cookies from 'next-cookies'; const c = cookies(context); const ...
Read more >
How to get cookies server-side in a Next.js app - Flavio Copes
I had this code, which was in charge of hitting a GET endpoint using Axios: Bookings.getInitialProps = async ctx => { const response...
Read more >
How to use cookies for persisting users in Nextjs
Finally, we will use getInitialProps in our component to check if the user already has a valid cookie on the server side before...
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