OpenID Connect & OAuth2 Client (TypeScript)

3.5.0 · active · verified Tue Apr 21

oidc-client-ts is an actively maintained TypeScript library, currently at version 3.5.0, providing OpenID Connect (OIDC) and OAuth2 protocol support for client-side, browser-based JavaScript applications. It includes robust features for user session and access token management. This project is a direct fork of the unmaintained `IdentityModel/oidc-client-js`, ported to TypeScript with a largely similar API for its 2.0 release. Moving forward, it prioritizes OAuth 2.1 protocols and explicitly *does not* support the deprecated implicit grant. A key change since v3.x is the transition from `crypto-js` to the browser's native `crypto.subtle` module for cryptographic operations, which mandates the use of secure contexts (HTTPS). The library supports various flows, including Authorization Code Grant with PKCE, Authorization Code Grant, Resource Owner Password Credentials (ROPC) Grant, Refresh Token Grant, and Silent Refresh Token in iframe Flow.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `UserManager`, configure basic OIDC settings, and handle the `signinRedirect` and `signinRedirectCallback` flow using a code example with TypeScript. It includes setting up logging and persistent user storage.

import { UserManager, WebStorageStateStore, Log } from 'oidc-client-ts';

// Configure logging for better debugging
Log.setLevel(Log.INFO);
Log.setLogger(console);

const userManagerSettings = {
  authority: 'https://demo.identityserver.io',
  client_id: 'interactive.public.js',
  redirect_uri: 'http://localhost:5003/signin-callback.html',
  post_logout_redirect_uri: 'http://localhost:5003/signout-callback.html',
  response_type: 'code',
  scope: 'openid profile api offline_access',
  // Use PKCE for enhanced security with authorization code flow
  client_secret: process.env.OIDC_CLIENT_SECRET ?? '', // Only required for confidential clients
  userStore: new WebStorageStateStore({ store: window.localStorage }),
  automaticSilentRenew: true,
  // Ensure this matches the origin of your app, especially for silent renew iframes
  monitorSession: true,
  checkSessionIntervalInSeconds: 3,
};

const userManager = new UserManager(userManagerSettings);

// Handle the signin redirect callback in your callback page
async function handleSigninCallback() {
  try {
    const user = await userManager.signinRedirectCallback();
    console.log('Signed in:', user);
    // Redirect to your application's main page or show user info
  } catch (error) {
    console.error('Error handling signin callback:', error);
  }
}

// Trigger signin (e.g., from a login button)
async function signIn() {
  try {
    await userManager.signinRedirect();
  } catch (error) {
    console.error('Error initiating signin:', error);
  }
}

// Example usage (assuming a simple HTML page):
// On your main page:
// document.getElementById('loginButton').addEventListener('click', signIn);

// On your signin-callback.html page:
// window.onload = handleSigninCallback;

// To simulate usage for demonstration:
if (window.location.pathname === '/signin-callback.html') {
  handleSigninCallback();
} else {
  console.log('User Manager initialized. Call signIn() to start login flow.');
  // For demonstration, directly call signIn if not on callback page
  // In a real app, this would be triggered by user action
  // signIn();
}

export { userManager, signIn, handleSigninCallback };

view raw JSON →