jwt-cookie
v1.0.1
Published
Small library for reading and writing objects to http requests and responses.
Downloads
15
Maintainers
Readme
JWT Cookie
Small library for reading and writing objects to http requests and responses.
Usage
npm install jwt-cookie
import JwtCookie from 'jwt-cookie';
const jwtc = new JwtCookie(
process.env.JWT_SECRET, // A secret string to use for signing the JWT
'my_session' // The cookie key to use (optional)
);
export default function requestHandler(req, res) {
const session = jwt.get(req) || { count: 0 };
session.count++;
jwt.set(res, session);
res.end(`You've viewed this page ${session.count} times.`)
}
API
JwtCookie
Class for reading objects from HTTP requests and saving objects to responses, using json web tokens under the hood
new JwtCookie(secret, cookieKey)
Create an instance of JwtCookie
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| secret | string | | The secret to use to generate and verify the web tokens |
| cookieKey | string | "session" | The key to use for the cookie, defaults to session
|
jwtCookie.get(req)
Get the object from the request. Returns null if none is set.
Kind: instance method of JwtCookie
| Param | Type | Description | | --- | --- | --- | | req | http.IncomingMessage | The incoming request object |
jwtCookie.set(res, payload)
Write an object to the response's cookie header
Kind: instance method of JwtCookie
| Param | Type | Description | | --- | --- | --- | | res | http.ServerResponse | The response object to write to | | payload | * | The object to write to the response cookie |