Electrode Cookies
Electrode isomorphic cookies lib.
Install
npm install electrode-cookies --save
Usage
This module offers reading and setting cookies in React code that works in both the browser or when doing Server Side Rendering.
In your pure server only code, you can also use this module to read and set cookies, but you MUST pass the request
object in the options. Otherwise an assert error will be thrown.
In NodeJS land:
const Cookies = require("electrode-cookies");
Reading cookies
In ReactJS land:
import Cookies from "electrode-cookies";
const value = Cookies.get("test-cookie");
In NodeJS land:
Note the difference is that request
is passed in options.
const Cookies = require("electrode-cookies");
const value = Cookies.get("test-cookie", { request });
Writing cookies
In ReactJS land:
import Cookies from "electrode-cookies";
Cookies.set( "foo", "bar", { path: "/", domain: ".walmart.com" } );
In NodeJS land:
Note the difference is that request
is passed in options.
const Cookies = require("electrode-cookies");
Cookies.set( "foo", "bar", { request, path: "/", domain: ".walmart.com" } );
Electrode Server Setup
The cookie writing on server side requires support from a Hapi plugin. If you use electrode-server, then it should have setup the plugin for you by default. Otherwise, you need to register the hapi plugin.
APIs
Cookies.get(key, [options])
Parameters:
key
- name of the cookieoptions
- (optional) Available for Server side only. options for getting the cookie
request
- The server request
object (Required on server).matchSubStr
- If true
, then do substring matching of key with all cookie keys.
skipEncoding
- (applies only if matchSubStr
is true
) If true
, then do not encode the key or decode the value.
Returns the value of the cookie for key
.
Cookies.set(key, value, [options])
Set a cookie with key
and value
.
Parameters:
key
- name of the cookievalue
- value of the cookieoptions
- (optional) options for the cookie
request
- On the server side, the request
object (Required on server).path
- string path of the cookie Default: "/"
domain
- string domain of the cookieexpires
- number of seconds the cookie will expiresecure
- A boolean of whether or not the cookie should only be available over SSL Default: falsehttpOnly
- A boolean of whether or not the cookie should only be available over HTTP(S) Default: falseforceAuthEncoding
- Forces non-standard encoding for +
and /
characters, use with auth cookies.skipEncoding
- Skip encoding/escaping of the cookie value. See source for details.
Cookies.expire(key, [options])
Expires a cookie specified by key
.
Parameters:
key
- name of the cookieoptions
- (optional) options for the cookie
path
- string path of the cookie Default: "/"
domain
- string domain of the cookiesecure
- A boolean of whether or not the cookie should only be available over SSL Default: falserequest
- The server request object (Required on server)