What is universal-cookie?
The universal-cookie npm package is a JavaScript library for managing cookies in both browser and Node.js environments. It provides a simple and universal API to handle cookies, making it easier to work with cookies across server-side and client-side without changing the codebase.
What are universal-cookie's main functionalities?
Get a cookie
This feature allows you to retrieve the value of a cookie by its name. It's useful for accessing user preferences or session data stored in cookies.
const cookies = new UniversalCookie();
const myCookie = cookies.get('myCookieName');
Set a cookie
This feature enables you to create a new cookie or update an existing one with a value, path, and maximum age. It's commonly used for storing user preferences or session tokens.
const cookies = new UniversalCookie();
cookies.set('myCookieName', 'myCookieValue', { path: '/', maxAge: 3600 });
Remove a cookie
This feature allows you to delete a cookie from the browser. It's useful for logging out users or clearing session data.
const cookies = new UniversalCookie();
cookies.remove('myCookieName', { path: '/' });
Other packages similar to universal-cookie
js-cookie
js-cookie is a simple, lightweight JavaScript API for handling cookies. It provides a straightforward way to create, read, and delete cookies on the client-side. Compared to universal-cookie, js-cookie is specifically designed for browser environments and does not support server-side rendering.
cookie
cookie is a Node.js module for parsing and serializing cookies. It's designed for server-side use, allowing for easy manipulation of HTTP cookies in Node.js applications. Unlike universal-cookie, cookie does not provide a unified API for both client and server environments.
cookies
cookies is a Node.js and browser module that provides a higher-level API for managing cookies in both environments. It offers features similar to universal-cookie but with additional options for security, such as automatic signing of cookies. It's a good alternative for applications requiring enhanced security measures.
universal-cookie
Universal cookies for JavaScript
![Test Status](https://github.com/github/docs/actions/workflows/test.yml/badge.svg)
Integrations
Getting started
npm install universal-cookie
or in the browser (global variable UniversalCookie
):
<script
crossorigin
src="https://unpkg.com/universal-cookie@3/umd/universalCookie.min.js"
></script>
API - Cookies class
Create a cookies context
- cookieHeader (string|object): specify the cookie header or object
get(name, [options])
Get a cookie value
- name (string): cookie name
- options (object):
- doNotParse (boolean): do not convert the cookie into an object no matter what
getAll([options])
Get all cookies
- options (object):
- doNotParse (boolean): do not convert the cookie into an object no matter what
set(name, value, [options])
Set a cookie value
- name (string): cookie name
- value (string|object): save the value and stringify the object if needed
- options (object): Support all the cookie options from RFC 6265
- path (string): cookie path, use
/
as the path if you want your cookie to be accessible on all pages - expires (Date): absolute expiration date for the cookie
- maxAge (number): relative max age of the cookie from when the client receives it in seconds
- domain (string): domain for the cookie (sub.domain.com or .allsubdomains.com)
- secure (boolean): Is only accessible through HTTPS?
- httpOnly (boolean): Is only the server can access the cookie? Note: You cannot get or set httpOnly cookies from the browser, only the server.
- sameSite (boolean|none|lax|strict): Strict or Lax enforcement
remove(name, [options])
Remove a cookie
- name (string): cookie name
- options (object): Support all the cookie options from RFC 6265
- path (string): cookie path, use
/
as the path if you want your cookie to be accessible on all pages - expires (Date): absolute expiration date for the cookie
- maxAge (number): relative max age of the cookie from when the client receives it in seconds
- domain (string): domain for the cookie (sub.domain.com or .allsubdomains.com)
- secure (boolean): Is only accessible through HTTPS?
- httpOnly (boolean): Is only the server can access the cookie? Note: You cannot get or set httpOnly cookies from the browser, only the server.
- sameSite (boolean|none|lax|strict): Strict or Lax enforcement
addChangeListener(callback)
Add a listener to when a cookie is set or removed.
- callback (function): Call that will be called with the first argument containing
name
, value
and options
of the changed cookie.
removeChangeListener(callback)
Remove a listener from the change callback.
Browser Example
import Cookies from 'universal-cookie';
const cookies = new Cookies();
cookies.set('myCat', 'Pacman', { path: '/' });
console.log(cookies.get('myCat'));
Server Example
import Cookies from 'universal-cookie';
const cookies = new Cookies(req.headers.cookie);
console.log(cookies.get('myCat'));