Skip to content

Overview

The WSO2 Identity Server Express SDK (@asgardeo/express) adds authentication to Express applications. It is built on the WSO2 Identity Server Node SDK and runs the OpenID Connect authorization code flow on the server. It adds sign-in and sign-out routes to your app, keeps the user's session in a cookie, and gives you middleware to protect routes.

Tip

Express apps should use this SDK rather than the Node SDK. Use the Node SDK only for Node.js apps that don't use Express.

Installation

Use one of the following options to install the WSO2 Identity Server Express SDK. The SDK reads the session cookie from req.cookies, so also install a cookie parser such as cookie-parser.

npm install @asgardeo/express cookie-parser
yarn add @asgardeo/express cookie-parser
pnpm add @asgardeo/express cookie-parser

The SDK works with Express 4.21.2 or later.

How it works

  1. AsgardeoExpressClient.getInstance(config) creates the client.
  2. AsgardeoExpressClient.asgardeoExpressAuth() returns a router that adds a sign-in route (/login by default) and a sign-out route (/logout by default).
  3. When the user opens the sign-in route, the SDK sets the ASGARDEO_SESSION_ID cookie and redirects them to WSO2 Identity Server. WSO2 Identity Server redirects them back to the same route, where the SDK gets the tokens, stores them against the session ID, and calls your onSignIn callback.
  4. AsgardeoExpressClient.protectRoute() returns middleware that only lets signed-in users through.
  5. In your routes, req.asgardeoAuth is the client. Pass it the session ID from req.cookies.ASGARDEO_SESSION_ID to read the user's information and tokens.

Example

Register an application in WSO2 Identity Server as a traditional web application, and add http://localhost:3000/login and http://localhost:3000/logout as authorized redirect URLs.

app.js
import express from "express";
import cookieParser from "cookie-parser";
import { AsgardeoExpressClient } from "@asgardeo/express";

const app = express();

app.use(cookieParser());

AsgardeoExpressClient.getInstance({
  appURL: "http://localhost:3000",
  baseUrl: "https://localhost:9443",
  clientId: "<your-app-client-id>",
  clientSecret: "<your-app-client-secret>"
});

// Adds the /login and /logout routes.
app.use(
  AsgardeoExpressClient.asgardeoExpressAuth(
    (res, tokenResponse) => {
      res.redirect("/profile");
    },
    (res) => {
      res.redirect("/");
    },
    (res, error) => {
      res.status(500).send(error.message);
    }
  )
);

// Only lets signed-in users through.
const requireAuth = AsgardeoExpressClient.protectRoute((res, reason) => {
  res.redirect("/login");

  return true;
});

app.get("/", (req, res) => {
  res.send(`<a href="/login">Sign in</a>`);
});

app.get("/profile", requireAuth, async (req, res) => {
  const user = await req.asgardeoAuth.getUser(req.cookies.ASGARDEO_SESSION_ID);

  res.send(`Hello ${user.username}. <a href="/logout">Sign out</a>`);
});

app.listen(3000);

The sign-in route is also the redirect URL that WSO2 Identity Server sends the authorization code to, and the sign-out route is where WSO2 Identity Server sends the user after sign-out. Both must be authorized redirect URLs of the application.

APIs

The SDK provides the following APIs.

AsgardeoExpressClient

AsgardeoExpressClient is the entry point of the SDK. Create the client with AsgardeoExpressClient.getInstance(), add its middleware to your app, and use it to read tokens and user information.

Middleware

Guides

Logging

The SDK writes debug logs to the console when the LOG_LEVEL environment variable is set to DEBUG, INFO, WARN or ERROR. Logging is off by default.

LOG_LEVEL=DEBUG node app.js