Socket
Socket
Sign inDemoInstall

universal-cookie

Package Overview
Dependencies
2
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

universal-cookie

Universal cookies for JavaScript


Version published
Maintainers
1
Weekly downloads
1,482,590
decreased by-6.73%

Weekly downloads

Package description

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

Readme

Source

Universal cookies for JavaScript

Integrations

Getting started

npm install universal-cookie

or in the browser (global variable UniversalCookie):

<script
  crossorigin
  src="https://unpkg.com/universal-cookie@7/umd/universalCookie.min.js"
></script>

API - Cookies class

constructor([cookieHeader], [defaultSetOptions])

Create a cookies context

  • cookieHeader (string|object): specify the cookie header or object
  • defaultSetOptions (object): specify the default options when setting cookies
    • 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
    • partitioned (boolean): Indicates that the cookie should be stored using partitioned storage

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
    • partitioned (boolean): Indicates that the cookie should be stored using partitioned storage

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
    • partitioned (boolean): Indicates that the cookie should be stored using partitioned storage

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.

update()

Read back the cookies from the browser and triggers the change listeners. This should normally not be necessary because this library detects cookie changes automatically.

Browser Example

import Cookies from 'universal-cookie';

const cookies = new Cookies(null, { path: '/' });

cookies.set('myCat', 'Pacman');
console.log(cookies.get('myCat')); // Pacman

Server Example

import Cookies from 'universal-cookie';

const cookies = new Cookies(req.headers.cookie, { path: '/' });

console.log(cookies.get('myCat')); // Pacman or undefined if not set yet

Keywords

FAQs

Last updated on 22 Feb 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc