Skip to content

Instantly share code, notes, and snippets.

@daliborgogic
Created July 24, 2017 16:56
Show Gist options
  • Save daliborgogic/ac0c1ac51c98e24b98b79662bec9a956 to your computer and use it in GitHub Desktop.
Save daliborgogic/ac0c1ac51c98e24b98b79662bec9a956 to your computer and use it in GitHub Desktop.
Get, set, delete cookie
const setCookie = (name, value, days = 7, path = '/') => {
const expires = new Date(Date.now() + days * 864e5).toUTCString()
document.cookie = `${name}=${encodeURIComponent(value)}; expires=${expires}; path=${path}`
}
const getCookie = name => {
return document.cookie.split('; ').reduce((r, v) => {
const parts = v.split('=')
return parts[0] === name ? decodeURIComponent(parts[1]) : r
}, '')
}
const deleteCookie = (name, path) => setCookie(name, '', -1, path)
// setCookie('token', this.$store.state.token, 10, this.$route.path)
// getCookie('token')
// deleteCookie('token')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment