3.1.0 • Published 2 years ago

next-universal-cookie v3.1.0

Weekly downloads
66
License
MIT
Repository
github
Last release
2 years ago

next-universal-cookie

NPM version NPM monthly download

Provides way to read, set and delete a cookie for Next.js similar to express such as req.cookies, res.cookie and res.clearCookie

Installation

npm install next-universal-cookie

Usage

With getServerSideProps and getInitialProps

import {GetServerSideProps, NextPageContext} from 'next';
import {applyServerSideCookie} from 'next-universal-cookie';

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

  // Typescript-ready

  // Parse all cookies
  const allCookies = req.cookies;

  // Set
  res.cookie();

  // Delete
  res.clearCookie();

  return {
    props: {},
  };
};

API Routes

// pages/api/index.ts
import {NextApiRequest, NextApiResponse} from 'next';
import {applyApiCookie} from 'next-universal-cookie';

export default function handler(
  req: NextApiRequest,
  res: NextApiResponse<{ok: boolean}>
) {
  applyApiCookie(req, res);

  // Typescript-ready
  const allCookies = req.cookies;
  res.cookie();
  res.clearCookie();

  // Response
  res.json({
    ok: true,
  });
}

API

import {applyServerSideCookie, applyApiCookie} from 'next-universal-cookie';

License

MIT

3.1.0

2 years ago

3.0.1

3 years ago

3.0.0

3 years ago

2.0.0

3 years ago

1.0.0

3 years ago

0.4.3

3 years ago

0.4.2

3 years ago

0.4.1

3 years ago

0.4.0

3 years ago

0.3.0

3 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago