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