-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcookie-manager.js
34 lines (32 loc) · 1.05 KB
/
cookie-manager.js
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
// Usage:
// run initCookieManager() before using
// Cookies Object is defined to global window object
// Use "Cookies['cookieName']" to get cookie
// Use "Cookies.set(name, value, expiry in days)" to set cookie
// Use "Cookies.remove(name)" to remove cookie
const initCookieManager = () => {
if (!window.Cookies) {
Object.defineProperty(window, 'Cookies', {
get: () => {
let allCookies = document.cookie.split('; ').reduce((cookies, cookie) => {
cookies[cookie.split('=')[0]] = unescape(cookie.split('=')[1]);
return cookies;
}, {});
allCookies.set = (name, value = '', days) => {
let expires = '';
if (days) {
let date = new Date();
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
expires = '; expires=' + date.toUTCString();
}
document.cookie = name + '=' + value + expires + '; path=/';
};
allCookies.remove = (name) => {
document.cookie = name + '=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
};
return allCookies;
},
});
}
};
export default initCookieManager;