# nookies

> A set of cookie helpers for Next.js

Latest version **2.5.2** (published 2021-01-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install nookies
pnpm add nookies
yarn add nookies
bun add nookies
```

## Health

**Score 35/100 (D)** — status: abandoned.

Positive: has types; no vulnerabilities; high maintenance score.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 2.5.2 |
| Published | 2021-01-21 |
| First published | 2018-01-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 21.3 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 2351 |
| Author | Matic Zavadlal |
| Maintainers | maticzav |
| Keywords | cookie, cookies, next, nextjs, js, auth, destory, parse, create, nookies |

## Links

- npm: https://www.npmjs.com/package/nookies
- Repository: https://github.com/maticzav/nookies
- Homepage: https://github.com/maticzav/nookies#readme
- Issues: https://github.com/maticzav/nookies/issues
- npm.io page: https://npm.io/package/nookies

## Dependencies (2)

- [cookie](https://npm.io/package/cookie.md) ^0.4.1
- [set-cookie-parser](https://npm.io/package/set-cookie-parser.md) ^2.4.6

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 2.5.2 (latest) — 2021-01-21
- 2.5.1 — 2021-01-16
- 2.5.0 — 2020-10-31
- 2.4.1 — 2020-10-22
- 2.4.0 — 2020-08-03
- 2.3.2 — 2020-06-22
- 2.3.1 — 2020-06-03
- 2.3.0 — 2020-05-06
- 2.2.4 — 2020-04-29
- 2.2.3 — 2020-04-24
- 2.2.2 — 2020-04-01
- 2.2.1 — 2020-03-29
- 2.2.0 — 2020-03-21
- 2.1.1 — 2020-03-10
- 2.0.8 — 2019-07-18
- … 19 more at https://npm.io/package/nookies/versions

## README

# nookies :cookie:

![Working](https://github.com/maticzav/nookies/workflows/Release/badge.svg)
[![npm version](https://badge.fury.io/js/nookies.svg)](https://badge.fury.io/js/nookies)

> A collection of cookie helpers for Next.js

## Features

- ✨ SSR support, for setter, parser and destroy
- ⚙️ Custom Express server support
- 🪶 super light
- 🛡 perfect for authentication

Setting and destroying cookies also works on server-side.

## Quick start

`yarn add nookies`

> You can play with the example code [here](https://codesandbox.io/s/charming-herschel-7z362).

### ServerSide cookies

```js
import nookies from 'nookies'

export default function Me() {
  return <div>My profile</div>
}

export async function getServerSideProps(ctx) {
  // Parse
  const cookies = nookies.get(ctx)

  // Set
  nookies.set(ctx, 'fromGetInitialProps', 'value', {
    maxAge: 30 * 24 * 60 * 60,
    path: '/',
  })

  // Destroy
  // nookies.destroy(ctx, 'cookieName')

  return { cookies }
}
```

## Client-only Cookies

```js
import { parseCookies, setCookie, destroyCookie } from 'nookies'

function handleClick() {
  // Simply omit context parameter.
  // Parse
  const cookies = parseCookies()
  console.log({ cookies })

  // Set
  setCookie(null, 'fromClient', 'value', {
    maxAge: 30 * 24 * 60 * 60,
    path: '/',
  })

  // Destroy
  // destroyCookie(null, 'cookieName')
}

export default function Me() {
  return <button onClick={handleClick}>Set Cookie</button>
}
```

## Custom Express server cookies

```js
const express = require('express');
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();
const { parseCookies, setCookie, destroyCookie } = require('nookies');

app.prepare()
    .then(() => {
        const server = express();

        server.get('/page', (req, res) => {

          // Notice how the request object is passed
          const parsedCookies = parseCookies({ req });

          // Notice how the response object is passed
          setCookie({ res }, 'fromServer', 'value', {
            maxAge: 30 * 24 * 60 * 60,
            path: '/page',
          });

          // destroyCookie({ res }, 'fromServer');

          return handle(req, res);
        });

    );
```

## Reference

> For client side usage, omit the `ctx` parameter. You can do so by setting it to an empty object (`{}`), `null` or `undefined`.

### `parseCookies(ctx, options)` or `nookies.get(ctx, options)`

- **ctx:** `Next.js context` || `(Express request object)`
- **options:**
  - **decode:** `a custom resolver function (default: decodeURIComponent)`

### `setCookie(ctx, name, value, options)` or `nookies.set(ctx, name, value, options)`

> Don't forget to end your response on the server with `res.send()`.

- **ctx:** `(Next.js context)` || `(Express request object)`
- **name:** cookie name
- **value:** cookie value
- **options:**
  - **domain**
  - **encode**
  - **expires**
  - **httpOnly**
  - **maxAge**
  - **path**
  - **sameSite**
  - **secure**

### `destroyCookie(ctx, name, options)` or `nookies.destroy(ctx, 'token', options)`

> Don't forget to end your response on the server with `res.send()`. This might be the reason your cookie isn't removed.

- **ctx:** `(Next.js context)` || `(Express response object)`
- **name:** cookie name
- **options:**
  - **domain**
  - **path**

## License

MIT

---
_Source: https://npm.io/package/nookies · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
