function cookieParser(secret, options) {
return function cookieParser(req, res, next) {
if (req.cookies) {
return next();
}
var cookies = req.headers.cookie;
var secrets = !secret || Array.isArray(secret)
? (secret || [])
: [secret];
req.secret = secrets[0];
req.cookies = Object.create(null);
req.signedCookies = Object.create(null);
// no cookies
if (!cookies) {
return next();
}
req.cookies = cookie.parse(cookies, options);
// parse signed cookies
if (secrets.length !== 0) {
req.signedCookies = signedCookies(req.cookies, secrets);
req.signedCookies = JSONCookies(req.signedCookies);
}
// parse JSON cookies
req.cookies = JSONCookies(req.cookies);
next();
};
}n/a
function JSONCookie(str) {
if (typeof str !== 'string' || str.substr(0, 2) !== 'j:') {
return undefined;
}
try {
return JSON.parse(str.slice(2));
} catch (err) {
return undefined;
}
}...
### cookieParser(secret, options)
- `secret` a string or array used for signing cookies. This is optional and if not specified, will not parse signed cookies. If
a string is provided, this is used as the secret. If an array is provided, an attempt will be made to unsign the cookie with each
secret in order.
- `options` an object that is passed to `cookie.parse` as the second option. See [cookie](https://www.npmjs.org/package/cookie)
for more information.
- `decode` a function to decode the value of the cookie
### cookieParser.JSONCookie(str)
Parse a cookie value as a JSON cookie. This will return the parsed JSON value if it was a JSON cookie, otherwise it will return
the passed value.
### cookieParser.JSONCookies(cookies)
Given an object, this will iterate over the keys and call `JSONCookie` on each value. This will return the same object passed in
.
...function JSONCookies(obj) {
var cookies = Object.keys(obj);
var key;
var val;
for (var i = 0; i < cookies.length; i++) {
key = cookies[i];
val = JSONCookie(obj[key]);
if (val) {
obj[key] = val;
}
}
return obj;
}...
- `options` an object that is passed to `cookie.parse` as the second option. See [cookie](https://www.npmjs.org/package/cookie)
for more information.
- `decode` a function to decode the value of the cookie
### cookieParser.JSONCookie(str)
Parse a cookie value as a JSON cookie. This will return the parsed JSON value if it was a JSON cookie, otherwise it will return
the passed value.
### cookieParser.JSONCookies(cookies)
Given an object, this will iterate over the keys and call `JSONCookie` on each value. This will return the same object passed in
.
### cookieParser.signedCookie(str, secret)
Parse a cookie value as a signed cookie. This will return the parsed unsigned value if it was a signed cookie and the signature
was valid, otherwise it will return the passed value.
...function cookieParser(secret, options) {
return function cookieParser(req, res, next) {
if (req.cookies) {
return next();
}
var cookies = req.headers.cookie;
var secrets = !secret || Array.isArray(secret)
? (secret || [])
: [secret];
req.secret = secrets[0];
req.cookies = Object.create(null);
req.signedCookies = Object.create(null);
// no cookies
if (!cookies) {
return next();
}
req.cookies = cookie.parse(cookies, options);
// parse signed cookies
if (secrets.length !== 0) {
req.signedCookies = signedCookies(req.cookies, secrets);
req.signedCookies = JSONCookies(req.signedCookies);
}
// parse JSON cookies
req.cookies = JSONCookies(req.cookies);
next();
};
}n/a
function signedCookie(str, secret) {
if (typeof str !== 'string') {
return undefined;
}
if (str.substr(0, 2) !== 's:') {
return str;
}
var secrets = !secret || Array.isArray(secret)
? (secret || [])
: [secret];
for (var i = 0; i < secrets.length; i++) {
var val = signature.unsign(str.slice(2), secrets[i]);
if (val !== false) {
return val;
}
}
return false;
}...
Parse a cookie value as a JSON cookie. This will return the parsed JSON value if it was a JSON cookie, otherwise it will return
the passed value.
### cookieParser.JSONCookies(cookies)
Given an object, this will iterate over the keys and call `JSONCookie` on each value. This will return the same object passed in
.
### cookieParser.signedCookie(str, secret)
Parse a cookie value as a signed cookie. This will return the parsed unsigned value if it was a signed cookie and the signature
was valid, otherwise it will return the passed value.
The `secret` argument can be an array or string. If a string is provided, this is used as the secret. If an array is provided, an
attempt will be made to unsign the cookie with each secret in order.
### cookieParser.signedCookies(cookies, secret)
...function signedCookies(obj, secret) {
var cookies = Object.keys(obj);
var dec;
var key;
var ret = Object.create(null);
var val;
for (var i = 0; i < cookies.length; i++) {
key = cookies[i];
val = obj[key];
dec = signedCookie(val, secret);
if (val !== dec) {
ret[key] = dec;
delete obj[key];
}
}
return ret;
}...
### cookieParser.signedCookie(str, secret)
Parse a cookie value as a signed cookie. This will return the parsed unsigned value if it was a signed cookie and the signature
was valid, otherwise it will return the passed value.
The `secret` argument can be an array or string. If a string is provided, this is used as the secret. If an array is provided, an
attempt will be made to unsign the cookie with each secret in order.
### cookieParser.signedCookies(cookies, secret)
Given an object, this will iterate over the keys and check if any value is a signed cookie. If it is a signed cookie and the signature
is valid, the key will be deleted from the object and added to the new object that is returned.
The `secret` argument can be an array or string. If a string is provided, this is used as the secret. If an array is provided, an
attempt will be made to unsign the cookie with each secret in order.
## Example
...function cookieParser(secret, options) {
return function cookieParser(req, res, next) {
if (req.cookies) {
return next();
}
var cookies = req.headers.cookie;
var secrets = !secret || Array.isArray(secret)
? (secret || [])
: [secret];
req.secret = secrets[0];
req.cookies = Object.create(null);
req.signedCookies = Object.create(null);
// no cookies
if (!cookies) {
return next();
}
req.cookies = cookie.parse(cookies, options);
// parse signed cookies
if (secrets.length !== 0) {
req.signedCookies = signedCookies(req.cookies, secrets);
req.signedCookies = JSONCookies(req.signedCookies);
}
// parse JSON cookies
req.cookies = JSONCookies(req.cookies);
next();
};
}n/a
function JSONCookie(str) {
if (typeof str !== 'string' || str.substr(0, 2) !== 'j:') {
return undefined;
}
try {
return JSON.parse(str.slice(2));
} catch (err) {
return undefined;
}
}...
### cookieParser(secret, options)
- `secret` a string or array used for signing cookies. This is optional and if not specified, will not parse signed cookies. If
a string is provided, this is used as the secret. If an array is provided, an attempt will be made to unsign the cookie with each
secret in order.
- `options` an object that is passed to `cookie.parse` as the second option. See [cookie](https://www.npmjs.org/package/cookie)
for more information.
- `decode` a function to decode the value of the cookie
### cookieParser.JSONCookie(str)
Parse a cookie value as a JSON cookie. This will return the parsed JSON value if it was a JSON cookie, otherwise it will return
the passed value.
### cookieParser.JSONCookies(cookies)
Given an object, this will iterate over the keys and call `JSONCookie` on each value. This will return the same object passed in
.
...function JSONCookies(obj) {
var cookies = Object.keys(obj);
var key;
var val;
for (var i = 0; i < cookies.length; i++) {
key = cookies[i];
val = JSONCookie(obj[key]);
if (val) {
obj[key] = val;
}
}
return obj;
}...
- `options` an object that is passed to `cookie.parse` as the second option. See [cookie](https://www.npmjs.org/package/cookie)
for more information.
- `decode` a function to decode the value of the cookie
### cookieParser.JSONCookie(str)
Parse a cookie value as a JSON cookie. This will return the parsed JSON value if it was a JSON cookie, otherwise it will return
the passed value.
### cookieParser.JSONCookies(cookies)
Given an object, this will iterate over the keys and call `JSONCookie` on each value. This will return the same object passed in
.
### cookieParser.signedCookie(str, secret)
Parse a cookie value as a signed cookie. This will return the parsed unsigned value if it was a signed cookie and the signature
was valid, otherwise it will return the passed value.
...function signedCookie(str, secret) {
if (typeof str !== 'string') {
return undefined;
}
if (str.substr(0, 2) !== 's:') {
return str;
}
var secrets = !secret || Array.isArray(secret)
? (secret || [])
: [secret];
for (var i = 0; i < secrets.length; i++) {
var val = signature.unsign(str.slice(2), secrets[i]);
if (val !== false) {
return val;
}
}
return false;
}...
Parse a cookie value as a JSON cookie. This will return the parsed JSON value if it was a JSON cookie, otherwise it will return
the passed value.
### cookieParser.JSONCookies(cookies)
Given an object, this will iterate over the keys and call `JSONCookie` on each value. This will return the same object passed in
.
### cookieParser.signedCookie(str, secret)
Parse a cookie value as a signed cookie. This will return the parsed unsigned value if it was a signed cookie and the signature
was valid, otherwise it will return the passed value.
The `secret` argument can be an array or string. If a string is provided, this is used as the secret. If an array is provided, an
attempt will be made to unsign the cookie with each secret in order.
### cookieParser.signedCookies(cookies, secret)
...function signedCookies(obj, secret) {
var cookies = Object.keys(obj);
var dec;
var key;
var ret = Object.create(null);
var val;
for (var i = 0; i < cookies.length; i++) {
key = cookies[i];
val = obj[key];
dec = signedCookie(val, secret);
if (val !== dec) {
ret[key] = dec;
delete obj[key];
}
}
return ret;
}...
### cookieParser.signedCookie(str, secret)
Parse a cookie value as a signed cookie. This will return the parsed unsigned value if it was a signed cookie and the signature
was valid, otherwise it will return the passed value.
The `secret` argument can be an array or string. If a string is provided, this is used as the secret. If an array is provided, an
attempt will be made to unsign the cookie with each secret in order.
### cookieParser.signedCookies(cookies, secret)
Given an object, this will iterate over the keys and check if any value is a signed cookie. If it is a signed cookie and the signature
is valid, the key will be deleted from the object and added to the new object that is returned.
The `secret` argument can be an array or string. If a string is provided, this is used as the secret. If an array is provided, an
attempt will be made to unsign the cookie with each secret in order.
## Example
...