{"library":"oidc-client","title":"OpenID Connect & OAuth2 Client","description":"oidc-client is a comprehensive JavaScript client library for OpenID Connect (OIDC) and OAuth2, designed to facilitate secure authentication and authorization in web applications. It handles complex OAuth2 flows, including Authorization Code Flow with PKCE, implicit flow, and refresh token management, abstracting away much of the underlying protocol complexity. Developed by IdentityModel, it maintains a strong focus on security and adherence to OIDC/OAuth2 specifications. The current stable version is 1.11.5, with an active development cycle characterized by frequent bug fix releases and minor feature updates approximately every 1-2 months, as evidenced by recent patch versions. Key differentiators include its robust handling of session management, silent token renewal, and extensive configurability, making it suitable for a wide range of single-page applications and client-side integrations.","language":"javascript","status":"active","last_verified":"Tue Apr 21","install":{"commands":["npm install oidc-client"],"cli":null},"imports":["import { UserManager } from 'oidc-client'","import { WebStorageStateStore } from 'oidc-client'","import { Log } from 'oidc-client'","import { OidcClient } from 'oidc-client'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { UserManager, WebStorageStateStore, Log } from 'oidc-client';\n\nLog.logger = console;\nLog.level = Log.INFO;\n\nconst settings = {\n  authority: 'https://demo.duendesoftware.com/', // Your OIDC provider authority\n  client_id: 'interactive.public', // Your client ID\n  redirect_uri: 'http://localhost:3000/callback', // Your app's redirect URI\n  response_type: 'code',\n  scope: 'openid profile api offline_access',\n  post_logout_redirect_uri: 'http://localhost:3000/', // Where to go after logout\n  userStore: new WebStorageStateStore({ store: window.localStorage }),\n  automaticSilentRenew: true,\n  // Optional: configure popup for signin/signout if needed\n  // popup_redirect_uri: 'http://localhost:3000/popup.html',\n  // popup_post_logout_redirect_uri: 'http://localhost:3000/popup.html',\n};\n\nconst userManager = new UserManager(settings);\n\nasync function signIn() {\n  try {\n    await userManager.signinRedirect();\n  } catch (error) {\n    Log.error(\"Signin error\", error);\n  }\n}\n\nasync function signOut() {\n  try {\n    await userManager.signoutRedirect();\n  } catch (error) {\n    Log.error(\"Signout error\", error);\n  }\n}\n\nasync function getUser() {\n  try {\n    const user = await userManager.getUser();\n    if (user) {\n      Log.info(\"User loaded:\", user);\n      console.log('Access Token:', user.access_token);\n      console.log('ID Token:', user.id_token);\n    } else {\n      Log.info(\"No user logged in.\");\n    }\n    return user;\n  } catch (error) {\n    Log.error(\"Error getting user:\", error);\n    return null;\n  }\n}\n\n// Example usage (e.g., in a SPA entry point)\nasync function initializeApp() {\n  const path = window.location.pathname;\n  if (path === '/callback') {\n    try {\n      const user = await userManager.signinRedirectCallback();\n      Log.info(\"Signin redirect callback processed. User:\", user);\n      window.history.replaceState({}, document.title, '/'); // Clean up URL\n    } catch (error) {\n      Log.error(\"Error in signin redirect callback\", error);\n    }\n  } else {\n    const user = await getUser();\n    if (!user) {\n      console.log('No user, initiating sign-in...');\n      signIn();\n    } else {\n      console.log('User already logged in.');\n      // For demonstration purposes, you might want to call sign out later\n      // setTimeout(signOut, 60000);\n    }\n  }\n}\n\ninitializeApp();\n","lang":"typescript","description":"Demonstrates the basic setup of UserManager for OIDC authentication, including configuration for sign-in, sign-out, and handling redirect callbacks for a Single Page Application (SPA).","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}