1.0.1 • Published 4 years ago

jwt-cookie v1.0.1

Weekly downloads
2
License
ISC
Repository
gitlab
Last release
4 years ago

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

ParamTypeDefaultDescription
secretstringThe secret to use to generate and verify the web tokens
cookieKeystring"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

ParamTypeDescription
reqhttp.IncomingMessageThe incoming request object

jwtCookie.set(res, payload)

Write an object to the response's cookie header

Kind: instance method of JwtCookie

ParamTypeDescription
reshttp.ServerResponseThe response object to write to
payload*The object to write to the response cookie