Skip to content
TesseraDocs
MenuExpress with openid-client

Express with openid-client

Sign users in to an Express app with Tessera, using the standard openid-client library and PKCE.

View as Markdown

On this page

Sign users in to an Express app with Tessera, using the standard openid-client library and PKCE.

Requires: Node.js 20.6 or later (for --env-file) and Express 4 or 5.

1. Create the app in Tessera

Sign in to the console at https://platform.kurthlabs.com/tessera with your invitation (see your first sign-in), open your realm and add an app. Choose Express and where it runs: this computer for now.

The console then shows what you need, each with a copy button:

  • the issuer, https://login.kurthlabs.com/realms/YOUR_REALM
  • the client ID
  • the redirect URI, http://localhost:3000/auth/callback
  • the client secret, once. Copy it now, or download the .env file. Tessera cannot show it again.

Put the values in .env.local as shown under Environment below, and keep that file out of git.

If you have the tessera CLI (during early access it is by invitation), tessera init does this step: it registers the app, writes .env.local and prints the code for your stack. The console path above needs no download.

2. Add sign-in

npm install express express-session openid-client@6

app.js

import express from "express";
import session from "express-session";
import * as oidc from "openid-client";

const e = process.env, redirect_uri = "http://localhost:3000/auth/callback";
const config = await oidc.discovery(new URL(e.TESSERA_ISSUER), e.TESSERA_CLIENT_ID, e.TESSERA_CLIENT_SECRET);
const app = express().use(session({ secret: e.SESSION_SECRET, resave: false, saveUninitialized: false }));

app.get("/login", async (req, res) => {
  const pkce = (req.session.pkce = oidc.randomPKCECodeVerifier()), state = (req.session.state = oidc.randomState());
  const code_challenge = await oidc.calculatePKCECodeChallenge(pkce);
  res.redirect(oidc.buildAuthorizationUrl(config, { redirect_uri, scope: "openid profile email", state, code_challenge, code_challenge_method: "S256" }).href);
});

app.get("/auth/callback", async (req, res) => {
  try {
    const tokens = await oidc.authorizationCodeGrant(config, new URL(req.originalUrl, `${req.protocol}://${req.get("host")}`), { pkceCodeVerifier: req.session.pkce, expectedState: req.session.state });
    req.session.user = tokens.claims();
    res.redirect("/");
  } catch (err) {
    console.error("sign-in failed:", err);
    res.status(401).send("sign-in failed");
  }
});

app.get("/", (req, res) => res.type("text").send(req.session.user ? `Signed in as ${req.session.user.email}` : "Not signed in"));

app.listen(3000);

Set "type": "module" in package.json, then run it with node --env-file=.env.local app.js (Bun loads .env.local on its own) and open /login. Approve on Tessera and you come back to / showing "Signed in as" and your email.

Environment

The console gives you the first three values (the downloaded .env file has them, and so does tessera init). Put them in .env.local:

TESSERA_ISSUER=https://login.kurthlabs.com/realms/YOUR_REALM
TESSERA_CLIENT_ID=YOUR_CLIENT_ID
TESSERA_CLIENT_SECRET=YOUR_CLIENT_SECRET
SESSION_SECRET=YOUR_SESSION_SECRET

SESSION_SECRET is yours, not Tessera's: it signs your app's own session cookie. Generate one with openssl rand -base64 32.

The redirect URI registered for this app is http://localhost:3000/auth/callback. For production, add your production address to the app's redirect URIs in the console (the same path on https://your-app.example); the local one can stay.