npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

simple-authentication-jwt

v1.0.3

Published

A JWT authentication utility package for Node.js and Express applications.

Readme


JWTAuth - Simple JWT Authentication Utility for Express

simple-authentication-jwt is a lightweight and easy-to-use utility for handling JWT authentication in Express applications. It provides methods to manage token verification, token extraction, and refresh token generation with minimal setup.

Features:

  • Check Authorization Header: Verifies if the Authorization header exists and extracts the Bearer token.
  • Verify Token: Validates JWT tokens to ensure they are correctly signed and unexpired.
  • Extract Data from Token: Easily extract specific fields from the decoded JWT payload.
  • Generate Refresh Token: Issues a refresh token to extend session validity.

Installation

You can install this utility in your Express project via npm:

npm i simple-authentication-jwt

Usage

1. Check Authorization Header

This middleware checks for the presence of the Authorization header and extracts the Bearer token.

import { Request, Response, NextFunction } from 'express';
import authenticationUtils from 'simple-authentication-jwt';

app.use(authenticationUtils.checkAuthHeader);

2. Verify Token

To protect your routes, use the verifyAuthHeader middleware. It verifies if the JWT token is valid and decodes the token to attach data to the request.

app.use(authenticationUtils.verifyAuthHeader);

3. Extract Data from Token

You can extract specific fields from the decoded JWT by calling the extractFieldsFromToken method, passing the token and the fields you need.

const fields = ['userId', 'role'];
const extractedData = authenticationUtils.extractFieldsFromToken(token, fields);

4. Generate Refresh Token

Generate a refresh token when needed, such as when the current token is about to expire. This will allow the user to stay authenticated without needing to log in again.

app.post('/generate-refresh-token', authenticationUtils.generateRefreshToken);

Configuration

Ensure that your environment variables are properly set to configure JWT secrets:

  • JWT_SECRET: Secret key used to sign and verify JWT tokens.
  • JWT_REFRESH_SECRET: Secret key for generating refresh tokens.
  • JWT_REFRESH_EXPIRY: Refresh token expiry time (default is 7d).
JWT_SECRET=your-jwt-secret
JWT_REFRESH_SECRET=your-refresh-token-secret
JWT_REFRESH_EXPIRY=7d

Example

Here’s an example of how to use the utility in an Express application.

import express from 'express';
import authenticationUtils from 'simple-authentication-jwt';
import dotenv from 'dotenv';

dotenv.config();

const app = express();
const port = 3000;

app.use(authenticationUtils.checkAuthHeader);
app.use(authenticationUtils.verifyAuthHeader);

app.get('/protected', (req, res) => {
  res.send('Protected Route Accessed');
});

app.post('/generate-refresh-token', authenticationUtils.generateRefreshToken);

app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});

Contributing

If you'd like to contribute to this project, feel free to submit a pull request or open an issue for any bugs or features you'd like to see.


License

This project is licensed under the MIT License. See the LICENSE file for more details.


Note: This utility is intended to simplify JWT authentication in your Express app. It abstracts common operations like token validation and extraction, enabling you to focus on building your app's features instead of spending time on authentication details.