Sat Mar 12 2022
1 views
2 min read
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?
import { GetServerSideProps } from 'next'export const getServerSideProps: GetServerSideProps = async (ctx) => {const { req, res } = ctxconst { cookies } = reqreturn { 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 here getServerSideProps
data fetching method. You can learn about data fetching methods of nextjs from
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.
© Copyright Cules Coding 2022