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

@zitadel/express-auth

v1.0.0

Published

A comprehensive Auth.js integration for Express applications with TypeScript support, framework-agnostic HTTP adapters, and role-based access control

Readme

Express Auth.js

An Express integration for Auth.js that provides seamless authentication with multiple providers, session management, and route protection using Express middleware patterns.

This integration brings the power and flexibility of Auth.js to Express applications with full TypeScript support and efficient HTTP handling.

Why?

Modern web applications require robust, secure, and flexible authentication systems. While Auth.js provides excellent authentication capabilities, integrating it with Express applications requires careful handling of request/response conversion and middleware composition.

However, a direct integration isn't always straightforward. Different types of applications or deployment scenarios might warrant different approaches:

  • HTTP Request Handling: Express uses its own request/response format which requires conversion to Web API standards used by Auth.js core. This integration handles that conversion transparently while maintaining proper encoding for different content types.
  • Middleware Composition: Express's middleware pattern requires proper error handling and next() chaining. This integration composes body parsing and authentication into a single middleware that works correctly with Express's middleware pipeline.
  • Session Management: Proper session handling requires integration with Express's request lifecycle. Manual integration often leads to inconsistent session management across different routes.

This integration, @zitadel/express-auth, aims to provide the flexibility to handle such scenarios. It allows you to leverage the full Auth.js ecosystem while maintaining Express best practices, ultimately leading to a more effective and less burdensome authentication implementation.

Installation

Install using NPM by using the following command:

npm install @zitadel/express-auth @auth/core

Usage

To use this integration, add the ExpressAuth middleware to your Express application. The middleware handles all Auth.js routes including sign-in, sign-out, and callbacks.

You'll need to configure it with your Auth.js providers and options.

First, add the middleware to your Express app:

import express from 'express';
import { ExpressAuth } from '@zitadel/express-auth';
import Zitadel from '@auth/core/providers/zitadel';

const app = express();
app.set('trust proxy', true);

app.use(
  '/auth/*',
  ExpressAuth({
    providers: [
      Zitadel({
        clientId: process.env.ZITADEL_CLIENT_ID,
        issuer: process.env.ZITADEL_ISSUER,
      }),
    ],
    secret: process.env.AUTH_SECRET,
    trustHost: true,
  }),
);

Using the Authentication System

The integration provides functions for handling authentication:

Functions:

  • ExpressAuth(): Middleware that handles all Auth.js routes
  • getSession(): Retrieves the current Auth.js session from requests

Basic Usage:

import { getSession } from '@zitadel/express-auth';
import type { Session } from '@auth/core/types';

// Public route - no authentication needed
app.get('/api/public', (req, res) => {
  res.json({ message: 'Public endpoint' });
});

// Protected route - manual session check
app.get('/api/profile', async (req, res) => {
  const session = await getSession(req, authConfig);

  if (!session) {
    return res.status(401).json({ error: 'Unauthorized' });
  }

  res.json({
    user: session.user,
    expires: session.expires,
  });
});
Example: Advanced Configuration with Multiple Providers

This example shows how to use the middleware with multiple Auth.js providers and custom session configuration:

import express from 'express';
import { ExpressAuth, getSession } from '@zitadel/express-auth';
import GoogleProvider from '@auth/core/providers/google';
import GitHubProvider from '@auth/core/providers/github';

const app = express();
app.set('trust proxy', true);

const authConfig = {
  providers: [
    GoogleProvider({
      clientId: process.env.GOOGLE_CLIENT_ID,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET,
    }),
    GitHubProvider({
      clientId: process.env.GITHUB_CLIENT_ID,
      clientSecret: process.env.GITHUB_CLIENT_SECRET,
    }),
  ],
  secret: process.env.AUTH_SECRET,
  trustHost: true,
  session: {
    strategy: 'jwt',
    maxAge: 30 * 24 * 60 * 60, // 30 days
  },
  callbacks: {
    jwt: async ({ token, user }) => {
      if (user) {
        token.roles = user.roles;
      }
      return token;
    },
    session: async ({ session, token }) => {
      session.user.roles = token.roles as string[];
      return session;
    },
  },
};

app.use('/auth/*', ExpressAuth(authConfig));

// Authentication middleware
async function requireAuth(req, res, next) {
  const session = await getSession(req, authConfig);
  if (!session?.user) {
    return res.redirect('/auth/signin?error=SessionRequired');
  }
  req.session = session;
  next();
}

// Protected routes
app.get('/api/user', requireAuth, (req, res) => {
  res.json(req.session.user);
});

Known Issues

  • Body Parsing: The middleware automatically applies JSON and URL-encoded body parsing. If you need custom body parsing, ensure it is configured before the Auth.js middleware.
  • Session Storage Configuration: The integration relies on Auth.js session handling mechanisms. When configuring custom session storage or database adapters, ensure they are properly configured in the Auth.js options passed to the middleware.

Useful links

  • Auth.js: The authentication library that this integration is built upon.
  • Express: The Node.js framework this integration is designed for.
  • Auth.js Providers: Complete list of supported authentication providers.

Contributing

If you have suggestions for how this integration could be improved, or want to report a bug, open an issue - we'd love all and any contributions.

License

Apache-2.0