A TypeScript library for encoding, decoding and validating JSON Web Tokens (JWTs) using the HS256 algorithm. This library includes functionality for encoding JWTs with a specified payload and secret, decoding JWTs to extract their contents, and validating JWTs.
- Encode JWTs with HS256 algorithm
- Decode JWTs and extract payload and header
- Validate JWTs
- Support for customizable payload expiration
- Supports both ES Modules and CommonJS modules
You can install this library via npm:
npm i @falgunpal/jwt-helper-ts
To create a JWT, use the encode_jwt function:
import { encode_jwt } from "@falgunpaljwt-helper-ts";
const secret = 'your-secret';
const id = 1;
const payload = { name: 'John Doe' };
const ttl = 3600; // Optional, in seconds
const token = encode_jwt(secret, id, payload, ttl);
console.log('Generated Token:', token);
To decode a JWT, use the decode_jwt function:
import { decode_jwt } from '@falgunpaljwt-helper-ts';
const secret = 'your-secret';
const token = 'your-jwt-token';
const decoded = decode_jwt(secret, token);
console.log('Decoded JWT:', decoded);
To validate a JWT, use the validate_jwt function:
import { validate_jwt } from '@falgunpaljwt-helper-ts';
const secret = 'your-secret';
const token = 'your-jwt-token';
const isValid = validate_jwt(secret, token);
console.log('Is Token Valid?', isValid);
This package supports both ECMAScript Modules (ESM) and CommonJS (CJS) module configurations. You can import and use the functions based on your module system preference.
If you're using ESM:
import { encode_jwt, decode_jwt, validate_jwt } from 'jwt-helper';
If you're using CommonJS:
const { encode_jwt, decode_jwt, validate_jwt } = require('jwt-helper');
The library includes tests for its core functionality. To run the tests, use:
npm test