-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCookieUtils.js
More file actions
52 lines (49 loc) · 1.43 KB
/
CookieUtils.js
File metadata and controls
52 lines (49 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
export const CookieUtils = {
get,
set,
del
};
/**
* Set a local cookie
* @memberof CookieUtils
* @param {string} cookieKey - The key for the cookie
* @param {mixed} value - The Value
* @param {number} expireInDays - Expiration date in days from now
* @param {string} path
*/
function set (cookieKey, value, expireInDays = 365, path) {
expireInDays = expireInDays;
const myDate = new Date();
const hostName = window.location.hostname;
const cookiePath = path || window.location.pathname;
myDate.setDate(myDate.getDate() + expireInDays);
let cookieStr = encodeURIComponent(cookieKey) + '=' +
encodeURIComponent(JSON.stringify(value)) +
';path=' + cookiePath +
';expires=' + myDate.toGMTString() +
';domain=' + hostName ;
// do not add SameSite when removing a cookie
if (expireInDays >= 0) cookieStr += ';SameSite=Strict';
document.cookie = cookieStr;
}
/**
* Return the value of a local cookie
* @memberof CookieUtils
* @param cookieKey - The key
*/
function get (cookieKey) {
const name = encodeURIComponent(cookieKey);
const value = '; ' + document.cookie;
const parts = value.split( name + '=');
if (parts.length < 2) return null;
const first = parts.pop().split(';').shift();
return JSON.parse(decodeURIComponent(first));
}
/**
* Delete a local cookie
* @memberof CookieUtils
* @param cookieKey - The key
*/
function del (cookieKey, path) {
set(cookieKey, { deleted: true }, -1, path);
}