Convert Figma logo to code with AI

IdentityModel logooidc-client-js

OpenID Connect (OIDC) and OAuth2 protocol support for browser-based JavaScript applications

2,429
842
2,429
116

Top Related Projects

OAuth 2 / OpenID Connect Client API for JavaScript Runtimes

OpenID Connect (OIDC) and OAuth2 protocol support for browser-based JavaScript applications

npm package for OpenID Connect, OAuth Code Flow with PKCE, Refresh tokens, Implicit Flow

Quick Overview

oidc-client-js is a JavaScript library for implementing OpenID Connect (OIDC) and OAuth2 protocols in browser-based applications. It provides a robust set of tools for handling authentication flows, managing tokens, and interacting with OIDC providers.

Pros

  • Easy integration with single-page applications (SPAs) and other browser-based clients
  • Supports various OIDC flows, including implicit, authorization code, and refresh token
  • Provides automatic silent token renewal and session management
  • Well-documented and actively maintained

Cons

  • Limited to browser environments, not suitable for server-side applications
  • May require additional configuration for complex authentication scenarios
  • Learning curve for developers new to OIDC concepts
  • Dependency on browser APIs may lead to compatibility issues with older browsers

Code Examples

  1. Creating a UserManager instance:
import { UserManager } from 'oidc-client-js';

const config = {
  authority: 'https://your-oidc-provider.com',
  client_id: 'your-client-id',
  redirect_uri: 'https://your-app.com/callback',
  response_type: 'code',
  scope: 'openid profile email'
};

const userManager = new UserManager(config);
  1. Initiating login:
userManager.signinRedirect()
  .then(() => console.log('Redirect to login page'))
  .catch(error => console.error('Error during login:', error));
  1. Handling the callback after login:
userManager.signinRedirectCallback()
  .then(user => {
    console.log('User logged in:', user);
    // Store user info or update application state
  })
  .catch(error => console.error('Error processing login response:', error));
  1. Retrieving the current user:
userManager.getUser()
  .then(user => {
    if (user) {
      console.log('User is logged in:', user);
    } else {
      console.log('User is not logged in');
    }
  })
  .catch(error => console.error('Error getting user:', error));

Getting Started

  1. Install the library:

    npm install oidc-client-js
    
  2. Import and configure UserManager:

    import { UserManager } from 'oidc-client-js';
    
    const config = {
      authority: 'https://your-oidc-provider.com',
      client_id: 'your-client-id',
      redirect_uri: 'https://your-app.com/callback',
      response_type: 'code',
      scope: 'openid profile email'
    };
    
    const userManager = new UserManager(config);
    
  3. Implement login and callback handling in your application, using the code examples provided above.

  4. For more advanced usage and configuration options, refer to the official documentation.

Competitor Comparisons

OAuth 2 / OpenID Connect Client API for JavaScript Runtimes

Pros of openid-client

  • More comprehensive OpenID Connect support, including advanced features like JWT-secured authorization
  • Better TypeScript support with full type definitions
  • More active development and frequent updates

Cons of openid-client

  • Steeper learning curve due to its more extensive API
  • Less browser-specific optimizations compared to oidc-client-js
  • Requires more setup and configuration for basic use cases

Code Comparison

oidc-client-js:

const settings = {
  authority: 'https://demo.identityserver.io',
  client_id: 'implicit',
  redirect_uri: 'https://localhost:5000/callback.html',
  response_type: 'id_token token',
  scope: 'openid profile email api'
};
const userManager = new Oidc.UserManager(settings);

openid-client:

const client = await Issuer.discover('https://demo.identityserver.io')
  .then(issuer => new issuer.Client({
    client_id: 'implicit',
    redirect_uris: ['https://localhost:5000/callback.html'],
    response_types: ['id_token token'],
  }));

Both libraries provide robust OpenID Connect implementations, but openid-client offers more advanced features and better TypeScript support at the cost of increased complexity. oidc-client-js may be more suitable for simpler use cases and browser-specific optimizations.

OpenID Connect (OIDC) and OAuth2 protocol support for browser-based JavaScript applications

Pros of oidc-client-ts

  • Written in TypeScript, providing better type safety and developer experience
  • More actively maintained with regular updates and bug fixes
  • Improved performance and smaller bundle size

Cons of oidc-client-ts

  • Potential breaking changes when migrating from oidc-client-js
  • May require additional configuration for TypeScript projects

Code Comparison

oidc-client-js:

import { UserManager } from 'oidc-client';

const userManager = new UserManager({
  authority: 'https://example.com',
  client_id: 'client_id',
  redirect_uri: 'https://app.example.com/callback'
});

oidc-client-ts:

import { UserManager } from 'oidc-client-ts';

const userManager = new UserManager({
  authority: 'https://example.com',
  client_id: 'client_id',
  redirect_uri: 'https://app.example.com/callback'
});

The code usage is similar between the two libraries, with the main difference being the import statement. oidc-client-ts uses the 'oidc-client-ts' package instead of 'oidc-client'.

Both libraries provide similar functionality for implementing OpenID Connect and OAuth2 in JavaScript applications. oidc-client-ts is essentially a TypeScript rewrite of oidc-client-js, offering improved type safety and maintenance. While the migration might require some adjustments, the benefits of using oidc-client-ts, especially in TypeScript projects, often outweigh the initial setup costs.

npm package for OpenID Connect, OAuth Code Flow with PKCE, Refresh tokens, Implicit Flow

Pros of angular-auth-oidc-client

  • Specifically designed for Angular applications, providing seamless integration
  • Offers more Angular-specific features and optimizations
  • Actively maintained with frequent updates and community support

Cons of angular-auth-oidc-client

  • Limited to Angular framework, less versatile for other JavaScript applications
  • May have a steeper learning curve for developers not familiar with Angular

Code Comparison

oidc-client-js:

var settings = {
  authority: 'https://demo.identityserver.io/',
  client_id: 'js',
  redirect_uri: 'https://myapp.com/callback.html',
  response_type: 'code',
  scope: 'openid profile api'
};
var mgr = new Oidc.UserManager(settings);

angular-auth-oidc-client:

@NgModule({
  imports: [
    AuthModule.forRoot({
      config: {
        authority: 'https://demo.identityserver.io',
        redirectUrl: window.location.origin,
        clientId: 'angular',
        scope: 'openid profile offline_access',
        responseType: 'code',
      },
    }),
  ],
})
export class AppModule {}

The code examples show the configuration setup for both libraries. oidc-client-js uses a more generic JavaScript approach, while angular-auth-oidc-client integrates directly with Angular's module system, demonstrating its framework-specific design.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

No Longer Maintained

This library, while functional, is no longer being maintained.

A successor project that is showing great progress in updating and modernizing is "oidc-client-ts" and can be found here.


npm package

oidc-client

Library to provide OpenID Connect (OIDC) and OAuth2 protocol support for client-side, browser-based JavaScript client applications. Also included is support for user session and access token management.

Install

Node.js

Node.js v4.4 or later required.

NPM

npm install oidc-client --save

NOTE: if you're not already using babel-polyfill make sure you run npm install --save babel-polyfill as well. Then include it in your build.

CommonJS

If you don't use a package manager or a module loader, then you can get the library from the dist folder on github here.

Including in the browser

If you intend to use this library directly in a browser and are not using UMD/AMD then there is a compiled version in the ~/dist folder. It is already bundled/minified and contains the necessary dependencies and polyfills (mainly for ES6 features such as Promises).

If you are using UMD/AMD and/or you already have included an ES6 polyfill (such as babel-polyfill.js) then you can include the UMD packaged version of the file from the ~/lib folder.

Building the Source

git clone https://github.com/IdentityModel/oidc-client-js.git
cd oidc-client-js
npm install
npm run build

Running the Sample

npm start

and then browse to http://localhost:15000.

Running the Tests

npm test

Docs

Some initial docs are here.

Feedback, Feature requests, and Bugs

All are welcome on the issue tracker.

NPM DownloadsLast 30 Days