A simple JS class to manage cookies
CookieManager provides a way to simply manage cookies.
Using npm, run the following command :
npm i @mercierkevin35/cookiemanager
Then use it in your file
import CookieManager from '@mercierkevin35/cookiemanager';
To create a cookie whose name is "myCookie" and value is "my cookie value",
proceed as follows :
//Create
const manager = new CookieManager(),
key = "myCookie",
value = "my cookie value";
manager.setCookie(key, value);
If not specified, the cookie expires at the end of the session. To explicitly specify the expiration delay, proceed like so :
//Create
const manager = new CookieManager(),
key = "myCookie",
value = "my cookie value",
expireDays = 7;
manager.setCookie(key, value, expireDays); // this cookie expires in 7 days
The getCookies method returns a JS object containing each cookie as attributes
You can get a specific value like this :
//Create
const manager = new CookieManager(),
key = "myCookie",
value = "my cookie value",
expireDays = 7;
manager.setCookie(key, value, expireDays); // this cookie expires in 7 days
//Read
let myValue = manager.getCookies().myCookie // myValue == "my cookie value"
You can also specify a key dynamically into brackets like this :
const manager = new CookieManager(),
key = "myCookie",
value = "my cookie value",
expireDays = 7;
manager.setCookie(key, value, expireDays); // this cookie expires in 7 days
//Read
let myValue = manager.getCookies().myCookie; // myValue == "my cookie value"
let dynamicKey = "myCookie";
let theValue = manager.getCookies()[dynamicKey]; // theValue == "my cookie value"
To update a cookie value, just set a cookie with the same key
//Create
const manager = new CookieManager(),
key = "myCookie",
value = "my cookie value",
expireDays = 7;
manager.setCookie(key, value, expireDays); // this cookie expires in 7 days
//Read
let myValue = manager.getCookies().myCookie; // myValue == "my cookie value"
let dynamicKey = "myCookie";
let theValue = manager.getCookies()[dynamicKey]; // theValue == "my cookie value"
//Update
manager.setCookie("myCookie", "my new cookie value");
To delete a cookie, use the deleteCookie method with the key as argument
//Create
const manager = new CookieManager(),
key = "myCookie",
value = "my cookie value",
expireDays = 7;
manager.setCookie(key, value, expireDays); // this cookie expires in 7 days
//Read
let myValue = manager.getCookies().myCookie; // myValue == "my cookie value"
let dynamicKey = "myCookie";
let theValue = manager.getCookies()[dynamicKey]; // theValue == "my cookie value"
//Update
manager.setCookie("myCookie", "my new cookie value");
//Delete
manager.deleteCookie("myCookie");
//myCookie no more exists