Encode, decode, and validate JSON Web Tokens (JWTs) with ease.
This library provides simple and efficient methods for working with JWTs in both JavaScript and TypeScript projects.
- Encode (create) JWT tokens
- Decode JWT tokens
- Validate JWT tokens
Check out the library's demo application or https://jwt-library.vercel.app/.
You can install the package via npm:
npm install @tanu-chahal/jwt-library
Or with yarn:
yarn add @tanu-chahal/jwt-library
First, import the methods you need:
import { encode_jwt, decode_jwt, validate_jwt } from '@tanu-chahal/jwt-library';
Creates a JWT token.
Arguments:
-
secret
(string, required): The secret key used to sign/encode the JWT. -
id
(string | number, required): An identifier (user ID, etc.) to include in the payload. -
payload
(object, required): The payload data you want to encode. -
ttl
(number, optional): Time to live in seconds. If provided, the token will have an expiration time.
Returns: A signed JWT token as a string.
Example:
const secret = 'your-secret-key';
const id = 'user123';
const payload = { role: 'admin', name: 'John Doe' };
const token = encode_jwt(secret, id, payload, 3600);
console.log(token);
Decodes a JWT token, verifies its signature & returns the encoded data and expiration date (if ttl
was passed while encoding).
Arguments:
-
secret
(string, required): The same secret key used to encode the JWT signature. -
jwt
(string, required): The JWT token to decode.
Returns: An object containing the id
, payload
, and expires_at
(as a Date or null).
Example:
const secret = 'your-secret-key';
const token = 'your-jwt-token';
try {
const decoded = decode_jwt(secret, token);
console.log(decoded);
} catch (error) {
console.error(error.message);
}
Validates a JWT token by decoding it and checking the signature.
Arguments:
-
secret
(string, required): The same secret key used to encode the JWT signature. -
jwt
(string, required): The JWT token to validate.
Returns: A boolean indicating whether the JWT is valid.
Example:
const secret = 'your-secret-key';
const token = 'your-jwt-token';
const isValid = validate_jwt(secret, token);
console.log(isValid); // true or false