Skip to content

Overview

The WSO2 Identity Server Node SDK (@asgardeo/node) adds authentication to server-side applications that run on Node.js. It handles the OpenID Connect authorization code flow with PKCE on the server, keeps each user's tokens in a server-side store keyed by a session ID that you choose, and gives you the user's tokens and information on later requests.

The SDK does not depend on a web framework. Your app owns the HTTP layer: it reads the session ID from a cookie, redirects the user, and calls the SDK. Use it with the Node.js http module or any framework that does not have its own WSO2 Identity Server SDK.

Tip

If you build with Express, use the WSO2 Identity Server Express SDK instead. It is built on this SDK and adds the sign-in and sign-out routes, the session cookie, and route protection for you. For Next.js, use the Next.js SDK.

Installation

Use one of the following options to install the WSO2 Identity Server Node SDK.

npm install @asgardeo/node
yarn add @asgardeo/node
pnpm add @asgardeo/node

How it works

  1. Your app gives each browser a session ID, usually in an HTTP-only cookie, and passes it to the SDK as userId.
  2. On the sign-in route, signIn() builds the authorization URL and hands it to a callback that you use to redirect the user to WSO2 Identity Server.
  3. WSO2 Identity Server redirects the user back to afterSignInUrl with an authorization code. Pass the code, session_state and state to signIn() to get the tokens. The SDK stores them against the session ID.
  4. On later requests, read the session ID from the cookie and call methods such as isSignedIn(), getUser() and getAccessToken() with it.
  5. To sign the user out, signOut() returns the WSO2 Identity Server sign-out URL to redirect the user to.

Example

The following example uses the Node.js http module. Register an application in WSO2 Identity Server as a traditional web application, and add http://localhost:3000/callback and http://localhost:3000 as authorized redirect URLs.

server.js
import { createServer } from "node:http";
import { randomUUID } from "node:crypto";
import { LegacyAsgardeoNodeClient } from "@asgardeo/node";

const APP_URL = "http://localhost:3000";
const COOKIE_NAME = "SESSION_ID";

const auth = new LegacyAsgardeoNodeClient();

await auth.initialize({
  afterSignInUrl: `${APP_URL}/callback`,
  afterSignOutUrl: APP_URL,
  baseUrl: "https://localhost:9443",
  clientId: "<your-app-client-id>",
  clientSecret: "<your-app-client-secret>"
});

// Reads the session ID from the request's cookies.
const getSessionId = (req) =>
  (req.headers.cookie ?? "")
    .split(";")
    .map((cookie) => cookie.trim().split("="))
    .find(([name]) => name === COOKIE_NAME)?.[1];

const redirect = (res, location) => {
  res.writeHead(302, { Location: location }).end();
};

createServer(async (req, res) => {
  const url = new URL(req.url, APP_URL);

  try {
    // Starts the sign-in flow.
    if (url.pathname === "/login") {
      const sessionId = getSessionId(req) ?? randomUUID();

      const tokens = await auth.signIn((authorizationUrl) => {
        res.setHeader("Set-Cookie", `${COOKIE_NAME}=${sessionId}; Path=/; HttpOnly; SameSite=Lax`);
        redirect(res, authorizationUrl);
      }, sessionId);

      // The user already has a valid session.
      if (tokens.accessToken) {
        redirect(res, "/");
      }

      return;
    }

    // Handles the redirect back from WSO2 Identity Server.
    if (url.pathname === "/callback") {
      const sessionId = getSessionId(req);

      // The user cancelled sign-in or WSO2 Identity Server returned an error.
      if (url.searchParams.has("error")) {
        res.statusCode = 401;
        res.end(url.searchParams.get("error_description") ?? "Sign-in failed.");

        return;
      }

      if (!sessionId) {
        redirect(res, "/login");

        return;
      }

      await auth.signIn(
        (authorizationUrl) => redirect(res, authorizationUrl),
        sessionId,
        url.searchParams.get("code") ?? undefined,
        url.searchParams.get("session_state") ?? undefined,
        url.searchParams.get("state") ?? undefined
      );

      if (!res.headersSent) {
        redirect(res, "/");
      }

      return;
    }

    // Signs the user out.
    if (url.pathname === "/logout") {
      const sessionId = getSessionId(req);

      if (!sessionId) {
        redirect(res, "/");

        return;
      }

      // Returns the sign-out URL and removes the user's tokens from the SDK's store.
      const signOutUrl = await auth.signOut(sessionId);

      res.setHeader("Set-Cookie", `${COOKIE_NAME}=; Path=/; Max-Age=0`);
      redirect(res, signOutUrl);

      return;
    }

    const sessionId = getSessionId(req);

    if (sessionId && (await auth.isSignedIn(sessionId))) {
      const user = await auth.getUser(sessionId);

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

      return;
    }

    res.end(`<a href="/login">Sign in</a>`);
  } catch (error) {
    res.statusCode = 500;
    res.end(error.message);
  }
}).listen(3000);

APIs

The SDK provides the following APIs.

LegacyAsgardeoNodeClient

LegacyAsgardeoNodeClient is the class that apps use. Create an instance, configure it with initialize(), and use it to sign users in and out and to read tokens and user information.

Framework integration APIs

The SDK also exports building blocks for writing a WSO2 Identity Server SDK for a Node.js framework, such as the base client class and session cookie helpers that the Next.js SDK uses. Apps do not need them.

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 server.js