forked from maticzav/nookies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (33 loc) · 905 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/* global document */
import cookie from 'cookie'
export function parseCookies(ctx = {}, options = {}) {
if (ctx.req && ctx.req.headers.cookie) {
return cookie.parse(ctx.req.headers.cookie, options)
}
if (process.browser) {
return cookie.parse(document.cookie, options)
}
return {}
}
export function setCookie(ctx = {}, name, value, options = {}) {
if (ctx && ctx.res) {
ctx.res.setHeader('Set-Cookie', cookie.serialize(name, value, options))
}
if (process.browser) {
document.cookie = cookie.serialize(name, value, options)
}
return {}
}
export function destroyCookie(ctx = {}, name) {
if (ctx && ctx.res) {
ctx.res.setHeader('Set-Cookie', cookie.serialize(name, '', {
maxAge: -1
}))
}
if (process.browser) {
document.cookie = cookie.serialize(name, '', {
maxAge: -1
})
}
return {}
}