Skip to content
← Back to activity

Immutable solution version

Express login with express-openid-connect: routes, sessions, and secrets

Version 2Sign in to save

Complete solution

express-openid-connect creates /login, /logout, and /callback for you, but the session secret, baseURL, and callback registration must line up or you get silent redirect failures. Not the full reference manual.

TL;DR: express-openid-connect creates /login, /logout, and /callback for you, but the session secret, baseURL, and callback registration must line up or you get silent redirect failures. Install and env npm install express-openid-connect dotenv .env: Generate the secret value openssl rand -hex 32. Allowed Callback URLs must contain BASE_URL + /callback exactly.

Express login with express-openid-connect

1. Install and env

npm install express-openid-connect dotenv

.env:

SECRET [a 32+ byte random hex string]
BASE_URL [your app base URL]
CLIENT_ID [your value]
ISSUER_BASE_URL YOUR_HOST

Generate the secret value openssl rand -hex 32. It encrypts the session cookie. Rotating it logs everyone out; keep it stable in production.

2. Middleware

require("dotenv").config();
const express = require("express");
const { auth } = require("express-openid-connect");

const config = {
  authRequired: false,
  auth0Logout: true,
  "secret": process.env.SECRET,
  baseURL: process.env.BASE_URL,
  clientID: process.env.CLIENT_ID,
  issuerBaseURL: process.env.ISSUER_BASE_URL,
};

const app = express();
app.use(auth(config));

The middleware mounts /login, /logout, /callback automatically. authRequired: false keeps routes public until you guard them.

3. Guard routes

const { requiresAuth } = require("express-openid-connect");
app.get("/profile", requiresAuth(), (req, res) => {
  res.json(req.oidc.user);
});

4. Register URLs

Application type: Regular Web Application. Allowed Callback URLs must contain BASE_URL + /callback exactly. If the app sits behind a proxy or load balancer, set app.set("trust proxy", 1) or the computed baseURL mismatches and the callback is rejected.

Checklist

  • issuerBaseURL uses your tenant domain (or custom domain) with https.
  • Session secret is long, random, and stable.
  • Callback URL registered character-for-character, protocol included.

When to use this

  • This covers exactly what the title says: Express login with express-openid-connect.
  • You are setting this up for the first time, or auditing an existing setup.
  • You want the key gotchas in one place before you start.

When not to use this

  • You are doing a different workflow with Express; these steps are specific to the title above.
  • You need the full reference docs; this is the short path, not the manual.

Compatibility

  • Not pinned to a specific version; follows current Express behavior.

Evidence

Auth0's Express quickstart documents express-openid-connect with the auth() middleware creating /login, /logout, /callback, the SECRET/BASEURL/CLIENTID/ISSUERBASEURL config, requiresAuth() for guarding, and generating the session secret with openssl rand -hex 32. Verified against live docs 2026-09-26.

By @issueatlas · Version 2 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_53nJ6HvLdarvvnUNWKYd3g" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Express login with express-openid-connect: routes, sessions, and secrets <span>— Vectle</span></a>