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

service-auth-express

v0.2.2

Published

**Express middleware wrapper for [`service-auth-core`](https://www.npmjs.com/package/service-auth-core)**

Readme

service-auth-express

Express middleware wrapper for service-auth-core

service-auth-express turns the low-level authentication & authorization primitives from service-auth-core into clean, reusable Express middleware.

It helps you:

  • avoid repeating JWT parsing and authorization logic in every route
  • keep authentication logic consistent across services
  • attach a typed req.auth context for business logic
  • separate auth concerns from business concerns

Why this package exists

service-auth-core already answers:

  • Is this JWT valid?
  • Is it expired?
  • Has the user’s role assignment changed?
  • Does the user have the required roles?

But in an Express service, every team still ends up writing the same glue code:

  • extract Bearer token
  • call doAuthorize
  • map auth errors to HTTP status codes
  • attach user context to req

service-auth-express standardizes this glue.


Installation

npm install service-auth-express service-auth-core

Concepts (quick mental model)

| Layer | Responsibility | | ---------------------- | ------------------------------------ | | service-auth-core | Authentication & authorization logic | | service-auth-express | HTTP / Express integration | | Your routes | Business logic only |


What this package provides (v0.1)

Middleware helpers

  • requireAuth() – authentication required
  • requireAnyRole() – user must have any of the roles
  • requireAllRoles() – user must have all roles
  • optionalAuth() – authentication optional

Typed request context

After successful auth, middleware attaches:

req.auth = {
  userId,
  roles,
  principal?,
  roleStamp?,
  roleVersion?,
  claims,
  token
}

TypeScript users get this automatically (no casting).


Basic usage

1️⃣ Create authCore using service-auth-core

import { createAuthCore, JwtTokenProvider } from "service-auth-core";

export const authCore = createAuthCore(
  {
    issuer: "auth-service",
    audience: ["my-api"],
    tokenTtlSeconds: 900,
    roleFreshness: { enabled: true },
  },
  {
    credentialChecker,
    roleProvider,
    roleStampProvider,
    roleVersionProvider,
    tokenProvider,
  },
);

requireAuth

app.get("/profile", requireAuth({ authCore }), (req, res) => {
  res.json({
    userId: req.auth!.userId,
    roles: req.auth!.roles,
  });
});

requireAnyRole

app.get(
  "/admin",
  requireAnyRole({ authCore }, "ADMIN", "SUPPORT"),
  (req, res) => {
    res.json({ message: "Admin access granted" });
  },
);

requireAllRoles

app.get(
  "/billing",
  requireAllRoles({ authCore }, "ADMIN", "BILLING_WRITE"),
  (req, res) => {
    res.json({ message: "Billing access granted" });
  },
);

optionalAuth

app.get("/products", optionalAuth({ authCore }), (req, res) => {
  if (req.auth) {
    res.json({ pricing: "contract pricing" });
  } else {
    res.json({ pricing: "public pricing" });
  }
});

Error → HTTP status mapping (default)

| Error code | HTTP | | --------------------- | ---- | | AUTH_TOKEN_INVALID | 401 | | AUTH_TOKEN_EXPIRED | 401 | | AUTH_TOKEN_STALE | 401 | | AUTH_FORBIDDEN | 403 | | AUTH_CONFIG_ERROR | 500 | | AUTH_INTERNAL_ERROR | 500 |


License

MIT