Created
July 24, 2018 11:52
-
-
Save NikaBuligini/b187e8d57a1d0854f7957fd20efc29f0 to your computer and use it in GitHub Desktop.
cookie.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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