forked from Hairfie/fluxible-plugin-cookie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
37 lines (31 loc) · 1.06 KB
/
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
'use strict';
var cookie = require('cookie');
function cookiePlugin() {
return {
name: 'CookiePlugin',
plugContext: function (options) {
var req = options.req;
var res = options.res;
var cookies = req ? req.cookies : cookie.parse(document.cookie);
function plugContext(actionContext) {
actionContext.setCookie = function (name, value, options) {
var cookieStr = cookie.serialize(name, value, options);
if (res) {
res.setHeader('Set-Cookie', cookieStr);
} else {
document.cookie = cookieStr;
}
cookies[name] = value;
};
actionContext.getCookie = function (name) {
return cookies[name];
}
}
return {
plugActionContext: plugContext,
plugStoreContext: plugContext
};
}
};
}
module.exports = cookiePlugin;