Access cookies in NextJS from server side

If you want to set an authentication system like jwt, then you have to store your token inside the client(browser). You store them either in localStorage or as a cookie. But when you perform any kind of server-side operation, then you don’t have access…


This content originally appeared on DEV Community and was authored by Anjan Shomooder

If you want to set an authentication system like jwt, then you have to store your token inside the client(browser). You store them either in localStorage or as a cookie. But when you perform any kind of server-side operation, then you don't have access to the client. So you can't access the cookies from the server-side. Then what is the solution?

The solution.

import { GetServerSideProps } from 'next'

export const getServerSideProps: GetServerSideProps = async (ctx) => {
    const { req, res } = ctx

    const {cookies} = req

    return { props: {  } }
}

I have created a YouTube video about this. You can check that out. If you like this video, please like and subscribe to my channel.

You can access the cookies from the request object inside the getServerSideProps data fetching method. You can learn about data fetching methods of nextjs from here .

getServerSideProps method takes context as a parameter. A context is a giant object. Request and Response object is inside the context object.

const { req, res } = ctx

In the request object you'll find a cookies object.

const { cookies } = req

All your cookies will be inside the cookies object.

So that's it for this blog.

Shameless Plug

I have made a video about how to build a carousel postcard with React, Material-UI, and Swiper.js.
If you are interested you can check the video.

You can also demo the application form here

Screenshot of Insta Carousel

Please like and subscribe to Cules Coding. It motivates me to create more content like this.

If you have any questions, please comment down below.
You can reach out to me on social media as @thatanjan .
Stay safe. Goodbye.

About me

Why do I do what I do?

The Internet has revolutionized our life. I want to make the internet more beautiful and useful.

What do I do?

I ended up being a full-stack software engineer.

What can I do?

I can develop complex full-stack web applications like social media applications or e-commerce sites.

What have I done?

I have developed a social media application called Confession. The goal of this application is to help people overcome their imposter syndrome by sharing our failure stories.
I also love to share my knowledge. So, I run a youtube channel called Cules Coding where I teach people full-stack web development, data structure algorithms, and many more. So, Subscribe to Cules Coding so that you don't miss the cool stuff.

Want to work with me?

I am looking for a team where I can show my ambition and passion and produce great value for them.
Contact me through my email or any social media as @thatanjan . I would be happy to have a touch with you.

Contacts

Blogs you might want to read:

Videos might you might want to watch:







This content originally appeared on DEV Community and was authored by Anjan Shomooder


Print Share Comment Cite Upload Translate Updates
APA

Anjan Shomooder | Sciencx (2021-09-02T12:43:00+00:00) Access cookies in NextJS from server side. Retrieved from https://www.scien.cx/2021/09/02/access-cookies-in-nextjs-from-server-side/

MLA
" » Access cookies in NextJS from server side." Anjan Shomooder | Sciencx - Thursday September 2, 2021, https://www.scien.cx/2021/09/02/access-cookies-in-nextjs-from-server-side/
HARVARD
Anjan Shomooder | Sciencx Thursday September 2, 2021 » Access cookies in NextJS from server side., viewed ,<https://www.scien.cx/2021/09/02/access-cookies-in-nextjs-from-server-side/>
VANCOUVER
Anjan Shomooder | Sciencx - » Access cookies in NextJS from server side. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/02/access-cookies-in-nextjs-from-server-side/
CHICAGO
" » Access cookies in NextJS from server side." Anjan Shomooder | Sciencx - Accessed . https://www.scien.cx/2021/09/02/access-cookies-in-nextjs-from-server-side/
IEEE
" » Access cookies in NextJS from server side." Anjan Shomooder | Sciencx [Online]. Available: https://www.scien.cx/2021/09/02/access-cookies-in-nextjs-from-server-side/. [Accessed: ]
rf:citation
» Access cookies in NextJS from server side | Anjan Shomooder | Sciencx | https://www.scien.cx/2021/09/02/access-cookies-in-nextjs-from-server-side/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.