@lokesh-kaushik/jwt-auth
v0.0.1
Published
Encode, Decode & Validate JSON Web Token (JWTs)
Downloads
2
Maintainers
Readme
@lokesh-kaushik/jwt-auth
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.
Features
- Encode (create) JWT tokens
- Decode JWT tokens
- Validate JWT tokens
Installation
You can install the package via npm:
npm install @lokesh-kaushik/jwt-auth
Or with yarn:
yarn add @lokesh-kaushik/jwt-auth
Usage
First, import the methods you need:
import { encode_jwt, decode_jwt, validate_jwt } from "@lokesh-kaushik/jwt-auth";
Methods
1. encode_jwt
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);
2. decode_jwt
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);
}
3. validate_jwt
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