@popovmp/jwt
v2.0.0
Published
JWT helpers for NodeJS
Downloads
11
Maintainers
Readme
JWT
A module for working with JWT (JSON Web Tokens) in NodeJS.
import {createJwt, getPayloadJwt, validateJwt, parseJwtPayload, isTokenJwt, encodeSha256} from "@popovmp/jwt";
const nowSec = Math.floor(Date.now() / 1000);
const oneHour = 60 * 60;
const payload = {
"iss": "example.com",
"iat": nowSec,
"exp": nowSec + oneHour,
"aud": "John",
};
// Create JWT with a key
const key = "foobar";
const jwt = createJwt(payload, key);
// Check if the JWT is valid
const isValid = validateJwt(jwt, key);
// Check if authorization header contains a JWT token
const authHeader = "Bearer " + jwt;
const isJWT = isTokenJwt(authHeader);
// Get the JWT payload
const decodedPayload = getPayloadJwt(jwt);
// Parse and validate Authorization header
const settings = {key: "foobar", iss: "example.com", aud: "John"};
const validatedPayload = parseJwtPayload(authHeader, settings);
// Low-level HMAC SHA-256 encoding (used internally)
const signature = encodeSha256("data", "secret-key");