How to use getInitialProps to get cookies?
See original GitHub issueI’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:
- Created 6 years ago
- Comments:7 (3 by maintainers)
Top 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 >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 FreeTop 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
Top GitHub Comments
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.
you can use
cookie-parser
package. Use it in server.js as follows:index.js