cookies-utils
v1.0.0
Published
A JavaScript API for handling cookies
Downloads
380
Maintainers
Readme
cookies-utils
This project contains functions to help manage cookies.
Installation
NPM
Install the library with npm install cookies-utils
.
CDN
Or use it directly in your browser via jsDelivr or unpkg:
<script src="https://cdn.jsdelivr.net/npm/cookies-utils/cookies-utils.min.js"></script>
...
cookiesUtils.deleteCookie('name')
or
<script src="https://unpkg.com/cookies-utils/cookies-utils.min.js"></script>
...
cookiesUtils.deleteCookie('name')
Usage
Set a cookie
import { setCookie } from "cookies-utils";
// more information about the options in documentation https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie
const cookieOptions = {
name: "name", // string,
value: "value", // string,
maxAge: 10 * 60, // optional number (value in seconds),
expires: new Date(2099, 10, 1), // optional Date,
path: "/path", // optional string,
domain: "site.com", // optional string,
secure: true, // optional boolean,
sameSite: "lax", // optional enum 'lax' | 'strict' | 'none'
};
setCookie(cookieOptions);
Check existence of cookie
import { cookieExists } from "cookies-utils";
const isExist = cookieExists("name");
Delete a cookie
import { deleteCookie } from "cookies-utils";
deleteCookie("name");
Check if cookie has specific value
import { cookieHasValue } from "cookies-utils";
const hasValue = cookieHasValue("name", "value");
Delete all cookies
import { deleteAllCookies } from "cookies-utils";
deleteAllCookies();