Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

jwt-smith

ndkariyasena12MIT1.0.1TypeScript support: included

Enhanced JWT Authentication and Authorization Module

jwt, authentication, authorization, token, refresh-token, jsonwebtoken, jwt-auth, jwt-middleware, token-management, jwt-verification, nodejs, typescript, express-middleware, security, auth, access-control, rbac, jsonwebtoken-utils, secure-jwt

readme

JWT Smith 🛡️

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

npm version License Build Status GitHub last commit GitHub commit activity Downloads


🚀 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 });
});
import { validateJwtCookieMiddleware } from 'jwt-smith';

app.use(validateJwtCookieMiddleware);

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

🧩 Middleware List

Middleware Description
validateJwtHeaderMiddleware Validates JWT from the Authorization header
validateJwtCookieMiddleware Validates JWT from cookies and refreshes tokens if needed
roleBasedAuthenticationMiddleware Restricts 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

changelog

jwt-smith

1.0.1

Patch Changes

  • 73e95d9: Changelog - Version 1.0.1

    • Debug logs have been added in the middleware functions.

1.0.0

Major Changes

  • c1ea438: Changelog - Version 1.0.0

    • Initial release of JWT Smith
    • Added JwtManager for centralized configuration management
    • Implemented sign and verify methods for JWT handling
    • Introduced validateJwtCookieMiddleware for token validation via cookies
    • Introduced validateJwtHeaderMiddleware for token validation via headers
    • Implemented roleBasedAuthenticationMiddleware for permission-based access control
    • Added support for custom token storage and middleware configurations
    • Provided a default in-memory token storage solution (not recommended for production)
    • Introduced .auth-permissions.json for defining role-based access controls
    • Included detailed documentation for each middleware and function