fix #216 with cookie mgmt.

This commit is contained in:
Cindy Chang
2024-06-14 16:44:17 +08:00
parent 5bf98a1b74
commit d0d6c482ee
3 changed files with 34 additions and 1 deletions

29
src/utils/cookieUtil.js Normal file
View File

@@ -0,0 +1,29 @@
export function getCookie(name) {
const nameEQ = name + "=";
const ca = document.cookie.split(';');
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1, c.length);
}
if (c.indexOf(nameEQ) === 0) {
return c.substring(nameEQ.length, c.length);
}
}
return null;
}
export function setCookie(name, value, days=1) {
let expires = "";
if (days) {
const date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
export function deleteCookie(name, path = '/') {
document.cookie = name + '=; Max-Age=-99999999; path=' + path;
}