Published on

How to get Request Cookies in Next.js getServerSideProps

Authors

Sometime you might want to get the request cookies in your app. Here is how to get cookies in Next.js getServerSideProps

export async function getServerSideProps(context) {
  const cookiesString = context.req.headers.cookie;
  const cookies = {};

  cookiesString.split(';').forEach((cookie) => {
    const [key, value] = cookie.split('=').map(c => c.trim());
    cookies[key] = value;
  });

  return {
    props: {}
  };
}

Happy reading cookies!