1.0.1 • Published 5 months ago

jwt-smith v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

JWT Smith šŸ›”ļø

A powerful, customizable, and secure JWT authentication module for Node.js.


šŸš€ Features

āœ… Easy to Use – Simple API for signing, verifying, and handling JWT tokens.
šŸ” Middleware Protection – Prebuilt Express middlewares for authentication and role-based access.
āš™ļø Customizable – Flexible token handling with blacklisting, rotation, and configuration options.
šŸ“Œ Secure – Supports token revocation, expiration, and advanced security best practices.
šŸ“š Well-Documented – Comprehensive documentation for smooth integration.


šŸ“š Installation

npm install jwt-smith

šŸ› ļø Usage

ā—ā—šŸŒ For a comprehensive guide and detailed information, please visit the official documentation website. JWT Smith Documentation

@Note ā— Debug logs have been added in the middleware functions to make the development process easier. It is highly recommended to disable debug logs in the production environment.

1ļøāƒ£ Initialize JWT Manager

import { JwtManager } from 'jwt-smith';

const jwtManager = new JwtManager({
	publicKey: process.env.PUBLIC_KEY || 'your-public-key',
	refreshTokenKey: process.env.REFRESH_TOKEN_KEY || 'your-refresh-key',
	signOptions: {
		algorithm: 'RS256',
		expiresIn: '1h',
	},
	verifyOptions: {
		algorithms: ['RS256'],
	},
	middlewareConfigs: {},
});

2ļøāƒ£ Sign a JWT Token

const token = await sign({
	payload: { id: 1, role: 'user' },
	secret: 'my-secret-key',
});

3ļøāƒ£ Verify a JWT Token

const decoded = await verify({
	token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...',
	secret: 'my-public-key',
});
console.log(decoded); // { id: "123", role: "admin", iat: ..., exp: ... }

4ļøāƒ£ Middleware for JWT Header Authentication

import express from 'express';
import { validateJwtHeaderMiddleware } from 'jwt-smith';

const app = express();
app.use(validateJwtHeaderMiddleware);

app.get('/protected', (req, res) => {
	res.json({ message: 'Access granted!', user: req.user });
});

5ļøāƒ£ Middleware for JWT Cookie Authentication

import { validateJwtCookieMiddleware } from 'jwt-smith';

app.use(validateJwtCookieMiddleware);

app.get('/secure', (req, res) => {
	res.json({ message: 'Secure route accessed!', user: req.user });
});

🧩 Middleware List

MiddlewareDescription
validateJwtHeaderMiddlewareValidates JWT from the Authorization header
validateJwtCookieMiddlewareValidates JWT from cookies and refreshes tokens if needed
roleBasedAuthenticationMiddlewareRestricts access based on user roles

šŸ”§ Configuration Options

JWT Smith provides customizable options for security and flexibility.

const jwtManager = new JwtManager({
	publicKey: process.env.PUBLIC_KEY || 'your-public-key',
	refreshTokenKey: process.env.REFRESH_TOKEN_KEY || 'your-refresh-key',
	signOptions: {
		algorithm: 'RS256',
		expiresIn: '1h',
	},
	verifyOptions: {
		algorithms: ['RS256'],
	},
	middlewareConfigs: {},
});

šŸ’¬ Community & Support

šŸ’” Documentation: Read the Docs
šŸ› Report Issues: GitHub Issues
🌟 Feature Requests: Discussions


🐜 License

This project is licensed under the MIT License - see the LICENSE file for details.


šŸŽÆ Contribute

We welcome contributions! Check out our CONTRIBUTING.md to get started.


šŸš€ Get Started with JWT Smith Today! šŸš€

npm install jwt-smith