jwt-smith v1.0.1
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
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