Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save NikaBuligini/b187e8d57a1d0854f7957fd20efc29f0 to your computer and use it in GitHub Desktop.

Select an option

Save NikaBuligini/b187e8d57a1d0854f7957fd20efc29f0 to your computer and use it in GitHub Desktop.
cookie.js
export const setCookie = (key, value, exdays) => {
const d = new Date();
d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);
const expires = `expires=${d.toUTCString()}`;
document.cookie = `${key}=${value};${expires};secure;path=/`;
};
export const getCookie = key => {
const name = `${key}=`;
const decodedCookie = decodeURIComponent(document.cookie);
const ca = decodedCookie.split(';');
for (let i = 0; i < ca.length; i += 1) {
let c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1);
}
if (c.indexOf(name) === 0) {
return c.substring(name.length, c.length);
}
}
return '';
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment