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

@mhiliger/auth-be

v1.0.4

Published

Shared authentication and JWT management for Express.js

Readme

@mhiliger/auth-be

Shared authentication backend for Express.js applications. This package provides a generic, database-agnostic framework for:

  • Secure JWT (JSON Web Token) issuance and verification.
  • User registration workflow (Sign up -> Verify Email -> Admin Approve -> Set Password).
  • Role-based access control middleware.
  • Extensible adapter pattern (HTTP or Database).

Installation

npm install @mhiliger/auth-be express jsonwebtoken express-rate-limit axios

Setup

1. Configure Adapter

The library uses an adapter pattern to interact with identity data. For most applications, you should use the HttpAdapter to delegate to a central identity service.

Using the HttpAdapter (Recommended):

This allows your application to authenticate users without connecting directly to the database.

const axios = require("axios");
const { createHttpAdapter } = require("@mhiliger/auth-be");

const authAdapter = createHttpAdapter(axios, process.env.IDENTITY_SERVICE_URL);

module.exports = authAdapter;

Using the built-in Postgres Adapter (Master Identity Service only):

The package includes a createPostgresAdapter for the service that owns the database. This requires a database connection object (e.g., from pg-promise).

// This should typically only be used by the central identity service
const { createPostgresAdapter } = require("@mhiliger/auth-be");
const db = require("./db"); // Your pg-promise database instance

const authAdapter = createPostgresAdapter(db);
module.exports = authAdapter;

2. Configure Email Service

The registration workflow requires an email service. Implement an object with the following methods:

const emailService = {
  sendVerificationEmail: async (user, token) => { ... },
  sendAdminNotification: async (user, adminEmail) => { ... },
  sendApprovalEmail: async (user, token) => { ... },
  sendRejectionEmail: async (user, reason) => { ... },
  sendPasswordResetEmail: async (user, token) => { ... }
};

3. Initialize Express App

Mount the auth and registration routers in your Express application.

const express = require("express");
const cookieParser = require("cookie-parser");
const { createAuthRouter, createRegistrationRouter, createVerifyJWT } = require("@mhiliger/auth-be");
const authAdapter = require("./db");
const emailService = require("./services/email");

const app = express();
app.use(express.json());
app.use(cookieParser());

const authConfig = {
  accessTokenSecret: process.env.ACCESS_TOKEN_SECRET,
  refreshTokenSecret: process.env.REFRESH_TOKEN_SECRET,
  accessTokenLife: "15m",
  refreshTokenLife: "1d",
};

// 1. Mount Auth Routes (Login, Refresh, Logout)
app.use("/api/auth", createAuthRouter({
  db: authAdapter,
  config: authConfig
}));

// 2. Configure JWT Verification Middleware
const verifyJWT = createVerifyJWT({
  accessTokenSecret: authConfig.accessTokenSecret,
  onVerifySuccess: (req, decoded) => {
    // Map decoded token data to request object
    req.user = decoded;
    req.permissions = decoded.permissions;
  }
});

// 3. Mount Registration Routes
app.use("/api/register", createRegistrationRouter({
  db: authAdapter,
  verifyJWT: verifyJWT,
  config: {
    verificationTokenLife: "24h",
    passwordSetupTokenLife: "48h",
    onRegistrationSubmit: (user, token) => emailService.sendVerificationEmail(user, token),
    onEmailVerified: (user) => emailService.sendAdminNotification(user, process.env.ADMIN_EMAIL),
    onApproval: (user, token) => emailService.sendApprovalEmail(user, token),
    onRejection: (user, reason) => emailService.sendRejectionEmail(user, reason),
    onPasswordReset: (user, token) => emailService.sendPasswordResetEmail(user, token),
  }
}));

app.listen(3000, () => console.log("Server running"));

API Reference

createAuthRouter({ db, config })

Creates authentication endpoints.

  • POST / (Login)
  • GET /refresh (Refresh Token)
  • POST /logout (Logout)

createRegistrationRouter({ db, verifyJWT, config })

Creates registration and password management endpoints.

  • POST /submit (Public: Initial registration)
  • GET /verify/:token (Public: Email verification)
  • POST /setup/:token (Public: Password setup)
  • GET /admin/list (Admin: List pending requests)
  • POST /admin/approve/:id (Admin: Approve request)
  • POST /admin/reject/:id (Admin: Reject request)