frontend/lib/cookies.ts (view raw)
1export const getCookie = (cname: string, cookieHeader?: string) => {
2 const cookieString =
3 typeof document === 'undefined' ? cookieHeader : document.cookie;
4 let name = cname + '=';
5 let decodedCookie = decodeURIComponent(cookieString || '');
6 let ca = decodedCookie.split(';');
7 for (let i = 0; i < ca.length; i++) {
8 let c = ca[i];
9 while (c.charAt(0) == ' ') {
10 c = c.substring(1);
11 }
12 if (c.indexOf(name) == 0) {
13 return c.substring(name.length, c.length);
14 }
15 }
16 return '';
17};
18
19export const setCookie = (cname: string, cvalue: string, exdays = 30) => {
20 if (typeof document !== 'undefined') {
21 const d = new Date();
22 d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);
23 let expires = 'expires=' + d.toUTCString();
24 document.cookie = cname + '=' + cvalue + ';' + expires + ';path=/';
25 }
26};