1.0.16 • Published 4 years ago

jwt-in-cookie v1.0.16

Weekly downloads
-
License
ISC
Repository
-
Last release
4 years ago

Description

Wrapper around the functionality of jsonwebtoken to easily set and validate JWT tokens in express requests/responses

QuickStart

1) Configuration

const jwtInCookie = require("jwt-in-cookie");
jwtInCookie.configure({secret: 'MY_SECRET'});

2) Setting JWT in request-cookie

function (req, res) {
    jwtInCookie.setJwtToken(res, data);
    ...
}

3) Validating JWT in response

function (req, res) {
    jwtInCookie.validateJwtToken(req);
    ...
}

4) Clear JWT token on user-logout

function (req, res) {
    jwtInCookie.clearToken(res);
    ...
}

API

jwtInCookie.configure(config)

config object literal

Configures the instance of jwt-in-cookie

Return: void

  • secret: string (required), secret to be used to sign the JWT

    Eg: abcdefghijklmnopqrstuvwxyz1234567890

  • timeoutDuration: string, length of time before signed data expires

    Eg: "2 hours", "1d", "20h", 60

jwtInCookie.setJwtToken(res, payload, cookieOptions)

Adds payload as a token in the response cookie using configured secret & options

Return: string, token of payload

res express response object

payload object

cookieOptions object, options that set in the cookie

  • httpOnly: boolean, javascript can't access the cookie
  • expires: number, time until the cookie expires (if 0, will expire at end of session)

jwtInCookie.clearToken(res)

Clears the jwt token from the response

Return: void

res express response object

jwtInCookie.validateJwtToken(req)

Returns decoded token if request contains a valid JWT in its cookie (must be preceded by jwtInCookie.configure)

Return: object

req express request object

jwtInCookie.encodePayload(payload)

Encodes payload using configured secret & options

Return: encoded payload

payload object

jwtInCookie.retrieveTokenFromCookie(req)

Retrieves decoded token from the input request's cookie (must be preceded by jwtInCookie.configure)

Return: decoded token

req express request object

1.0.16

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.11

4 years ago

1.0.12

4 years ago

1.0.9

4 years ago

1.0.10

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago