Express Quickstart
Use this guide to add Choreo Identity & Access Management authentication to an Express app with sign-in, sign-out, and route protection.
What You Will Learn
- Create a new Express app
- Install the
@thunderid/expresspackage - Add working sign-in and sign-out routes
- Protect routes and access the signed-in user
Prerequisites
- About 15 minutes
- Node.js installed on your system
- npm, yarn, or pnpm
- Your preferred code editor
Create an Application
- Sign in to the Console at https://console.cloud.choreolabs.com/iam/. Select your project in the project switcher in the header.
- Navigate to Applications.
- Click Add application.
- From the Choose a type page, select Express.
- Enter a name (e.g.
My Express App). - Click Create.
- Select how you want to sign in users (e.g., email/password, social login, etc.).
- Select Theme settings.
- Leave Sign-in approach set to Redirect to Choreo Identity & Access Management Gate (the default for Express applications).
- In the Configuration step, add
http://localhost:3000/loginto the list of Authorized redirect URIs.
Copy both the Client ID and Client secret from the window that pops up when the application is created. The Client ID can also be found in the General tab.
Code in this guide uses https://<DATA_PLANE_URL> for your environment's data plane URL, where users sign in and tokens are issued. Replace it with the URL on the environment's Endpoints page. To open it, select your project, go to Deployment pipeline, and click Open environment on the environment's card. See Control Plane and Data Plane URLs.
Saving a change does not make it live. You author in the control plane, and your users sign in on a separate data plane, so a change takes effect in an environment only after you deploy it. Go to Deploy, deploy the latest version to your first environment, then promote it to the next environment when you are ready.
Auto-sync deploys saved changes to your first environment within seconds. Turning it on is your choice, per project. See Deploy a Version, or learn why you deploy and how auto-sync works.
Create an Express App
Create your new Node.js application:
- npm
- Yarn
- pnpm
- Bun
- Deno
mkdir my-express-app
cd my-express-app
npm init -y
npm install express cookie-parser
mkdir my-express-app
cd my-express-app
yarn init -y
yarn add express cookie-parser
mkdir my-express-app
cd my-express-app
pnpm init
pnpm add express cookie-parser
mkdir my-express-app
cd my-express-app
bun init -y
bun add express cookie-parser
mkdir my-express-app
cd my-express-app
deno init
deno add npm:express npm:cookie-parser
Install the SDK and Dependencies
Install the Choreo Identity & Access Management Express SDK:
- npm
- Yarn
- pnpm
- Bun
- Deno
npm install @thunderid/express
yarn add @thunderid/express
pnpm add @thunderid/express
bun add @thunderid/express
deno add npm:@thunderid/express
Add Authentication Middleware and Routes
Create an index.js file with Choreo Identity & Access Management middleware and auth routes:
const express = require('express');
const cookieParser = require('cookie-parser');
const {thunderID, handleSignIn, handleSignOut, protect} = require('@thunderid/express');
const app = express();
const port = 3000;
app.use(cookieParser());
app.use(express.json());
app.use(
thunderID({
baseUrl: 'https://<DATA_PLANE_URL>',
clientId: '<your-client-id>',
clientSecret: '<your-client-secret>',
afterSignInUrl: 'http://localhost:3000/login',
afterSignOutUrl: 'http://localhost:3000/logout',
}),
);
app.get('/', (_req, res) => {
res.send('<a href="/protected">Go to protected page</a>');
});
app.get('/login', handleSignIn());
app.get('/logout', handleSignOut());
app.get(
'/protected',
protect((res) => res.redirect('/login')),
(_req, res) => {
res.send('You are signed in and can access this protected route.');
},
);
app.get('/me', protect(), async (req, res) => {
const user = await req.thunderIDAuth.getUserFromRequest(req);
res.json(user);
});
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});
Replace <your-client-id> and <your-client-secret> with values from your Choreo Identity & Access Management application. Make sure the authorized redirect URL in your application settings is set to http://localhost:3000/login.
The deno init scaffold above does not create a package.json with "type": "commonjs", so Deno treats a .js file as an ES module and the require() calls above fail with require is not defined. Save the file as index.mjs instead, using import in place of require:
import express from "express";
import cookieParser from "cookie-parser";
import {thunderID, handleSignIn, handleSignOut, protect} from '@thunderid/express';
const app = express();
const port = 3000;
app.use(cookieParser());
app.use(express.json());
app.use(
thunderID({
baseUrl: 'https://<DATA_PLANE_URL>',
clientId: '<your-client-id>',
clientSecret: '<your-client-secret>',
afterSignInUrl: 'http://localhost:3000/login',
afterSignOutUrl: 'http://localhost:3000/logout',
}),
);
app.get('/', (_req, res) => {
res.send('<a href="/protected">Go to protected page</a>');
});
app.get('/login', handleSignIn());
app.get('/logout', handleSignOut());
app.get(
'/protected',
protect((res) => res.redirect('/login')),
(_req, res) => {
res.send('You are signed in and can access this protected route.');
},
);
app.get('/me', protect(), async (req, res) => {
const user = await req.thunderIDAuth.getUserFromRequest(req);
res.json(user);
});
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});
Run it with deno run -A index.mjs in the next step, instead of index.js.
Run Your App
Start the server:
- npm
- Yarn
- pnpm
- Bun
- Deno
node index.js
yarn node index.js
pnpm node index.js
bun index.js
deno run -A index.mjs
Open http://localhost:3000/protected.
You should be redirected to Choreo Identity & Access Management sign-in. After successful login, you'll return to your app and access the protected route. Then open http://localhost:3000/me to inspect the signed-in user profile.
You'll need a user to sign in with. If you haven't created one yet, open https://console.cloud.choreolabs.com/iam/, navigate to Users, and add a test user with an email and password.
You should see the sign-in link. Click it to be redirected to the Choreo Identity & Access Management-hosted sign-in page. After authenticating with your test user, you'll return to the app as a signed-in user.
Run the Sample App
The Express sample is a working version of the app this guide builds. To run it instead of building the app yourself:
-
Download the sample from its releases and open its folder.
-
Create
.envin the sample's folder with your application's values:.envTHUNDERID_BASE_URL=https://<DATA_PLANE_URL>THUNDERID_CLIENT_ID=<your-client-id>THUNDERID_CLIENT_SECRET=<your-client-secret> -
Install the dependencies and start the app:
npm installnpm start -
Open http://localhost:3000/login and sign in.
What's Next
- Upgrade the sign-in flow: Add MFA, passkeys, or social login to your flow without changing your route wiring.
- Express SDK Reference: Explore the full API reference for the Express SDK.
Was this page helpful?