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

authpress

v1.0.2

Published

Multi-auth middleware for Express: JWT, Basic, API Key, OAuth (Google, GitHub, Facebook, Discord)

Readme

🛡️ AuthPress

npm npm downloads

Multi-auth middleware for Express.js: JWT, Basic, API Key, OAuth (Google, GitHub, Facebook, Discord). Supports session or JWT mode for OAuth.


Prerequisites

  • Node.js >= 18
  • Express.js >= 4
  • .env file must include:
# --- GOOGLE OAUTH ---
GOOGLE_CLIENT_ID=your_google_client_id
GOOGLE_CLIENT_SECRET=your_google_client_secret
GOOGLE_CALLBACK_URL=http://localhost:3000/auth/google/callback

# --- GITHUB OAUTH ---
GITHUB_CLIENT_ID=your_github_client_id
GITHUB_CLIENT_SECRET=your_github_client_secret
GITHUB_CALLBACK_URL=http://localhost:3000/auth/github/callback

# --- FACEBOOK OAUTH ---
FACEBOOK_CLIENT_ID=your_facebook_app_id
FACEBOOK_CLIENT_SECRET=your_facebook_app_secret
FACEBOOK_CALLBACK_URL=http://localhost:3000/auth/facebook/callback

# --- DISCORD OAUTH ---
DISCORD_CLIENT_ID=your_discord_client_id
DISCORD_CLIENT_SECRET=your_discord_client_secret
DISCORD_CALLBACK_URL=http://localhost:3000/auth/discord/callback

# --- MODE (select one: session | jwt) ---
AUTH_MODE=jwt

# --- JWT CONFIG (if you use mode=jwt) ---
JWT_SECRET=supersecretkey
JWT_EXPIRES_IN=1h

# --- JWT SECRET ---
JWT_SECRET=yourjwtsecret

Installation

npm install authpress

Authentication Types

  1. JWT – Stateless, best for SPA or API.
  2. Basic – Simple username/password, good for internal tools.
  3. API Key – Header-based authentication, ideal for service-to-service communication.
  4. OAuth – Login via providers (Google, GitHub, Facebook, Discord), supports session or JWT mode.

Usage

const express = require("express");
const session = require("express-session");
const auth = require("authpress");
const applyAuth = auth.applyAuth;

const app = express();
app.use(express.json());
app.use(session({ secret: "secret", resave: false, saveUninitialized: true }));

// Initialize OAuth (only required for session mode)
auth.oauth.init(app);

// Setup providers
auth.oauth.oauth([
  {
    provider: "google",
    clientId: process.env.GOOGLE_CLIENT_ID,
    clientSecret: process.env.GOOGLE_CLIENT_SECRET,
    callbackURL: process.env.GOOGLE_CALLBACK,
    mode: "jwt",
    jwtSecret: process.env.JWT_SECRET,
    jwtExpiresIn: "2h" // default "1h"
  },
  {
    provider: "github",
    clientId: process.env.GITHUB_CLIENT_ID,
    clientSecret: process.env.GITHUB_CLIENT_SECRET,
    callbackURL: process.env.GITHUB_CALLBACK,
    mode: "session"
  }
]);

// JWT route
app.get("/jwt", auth.jwt({ secret: process.env.JWT_SECRET }), (req, res) => {
  res.json({ message: "JWT Auth OK", user: req.user });
});

// JWT Login (generate token)
app.post('/login', (req, res) => {
  const user = { id: 1, username: 'admin', role: 'admin' };
  const token = auth.jwt.sign(user, config.secret, { expiresIn: '1h' });
  res.json({ token });
});

// JWT route with role
app.get('/admin', auth.jwt({ secret: process.env.JWT_SECRET }), auth.jwt.withRole(['admin']), (req, res) => {
  res.json({ message: 'Admin Dashboard' });
});

// JWT Refresh token
app.post('/refresh', (req, res) => {
  const { refreshToken } = req.body;
  try {
    const newToken = auth.jwt.refresh(refreshToken, config.secret, { expiresIn: '1h' });
    res.json({ token: newToken });
  } catch {
    res.status(401).json({ error: 'Invalid refresh token' });
  }
});

// Basic Auth route
app.get("/basic", auth.basic({ users: { admin: "1234" } }), (req, res) => {
  res.json({ message: "Basic Auth OK", user: req.user });
});

// API Key route
app.get("/apikey", auth.apikey({ keyHeader: "x-api-key", keys: ["123456"] }), (req, res) => {
  res.json({ message: "API Key OK" });
});

// OAuth routes
app.get("/auth/google", auth.oauth.login("google"));
app.get("/auth/google/callback", auth.oauth.callback("google"));

// Multi-auth setup (optional)
const routes = [
  { method: "get", path: "/jwt", type: "jwt", config: { secret: process.env.JWT_SECRET }, handler: (req,res)=>res.json({user:req.user}) },
  { method: "get", path: "/basic", type: "basic", config: { users: { admin: "1234" } }, handler: (req,res)=>res.json({user:req.user}) },
  { method: "get", path: "/apikey", type: "apikey", config: { keyHeader: "x-api-key", keys: ["123456"] }, handler: (req,res)=>res.json({message:"API Key OK"}) },
  { method: "get", path: "/auth/google", type: "oauth", config: { provider: "google" }, handler: (req,res)=>{} },
  { method: "get", path: "/auth/google/callback", type: "oauth", config: { provider: "google", successRedirect: "/", failureRedirect: "/" }, handler: (req,res)=>{} }
];

applyAuth(app, routes);

app.listen(3000, () => console.log("✅ Server running at http://localhost:3000"));

Tips

  • Use mode: "jwt" for SPAs or mobile apps.
  • Use mode: "session" for traditional web applications.
  • Supports multiple OAuth providers at once.
  • JWT can be customized via jwtExpiresIn and jwtSecret.
  • All authentication routes can be managed from a single routes array → clean & scalable.

License

🛡️ AuthPress is released under the MIT License.