react-oidc-context is a lightweight authentication library for React Single Page Applications (SPAs) that leverages the React Context API for state management, built on top of the `oidc-client-ts` library. The current stable version is 3.3.1. The package maintains a fairly active release cadence, with frequent bugfix and minor releases, and significant major versions introducing breaking changes as the underlying `oidc-client-ts` library evolves. It differentiates itself by providing convenient React hooks (`useAuth`) and Higher-Order Components (`withAuthenticationRequired`) to integrate OpenID Connect and OAuth2 authentication flows seamlessly into React components, handling aspects like token renewal and redirect callbacks without requiring manual route setup. It ships with full TypeScript support, making it suitable for modern React development.
npm install react-oidc-contextVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up `AuthProvider` to configure OpenID Connect and use the `useAuth` hook within a functional component to manage authentication state (loading, authenticated, user info) and trigger authentication actions like login and logout. It highlights the critical `onSigninCallback` for proper token renewal.
Ensure your Node.js environment is version 18 or higher. Update `oidc-client-ts` to a compatible v3.x version (`^3.1.0` or later).
Always run your application under HTTPS, even during local development (e.g., via `mkcert` or a reverse proxy like `nginx`).
Add an `onSigninCallback` to your `oidcConfig` that uses `window.history.replaceState({}, document.title, window.location.pathname);` to clean the URL.Ensure your project is configured for ESM and use `import` statements for all `react-oidc-context` imports.
Serve your application over HTTPS. For local development, configure your server to use SSL/TLS or use a tool like `mkcert`.
Implement the `onSigninCallback` function in your `oidcConfig` to clear the URL parameters, e.g., `onSigninCallback: () => { window.history.replaceState({}, document.title, window.location.pathname); }`.Update your import statements to use ESM `import` syntax (e.g., `import { AuthProvider } from 'react-oidc-context';`) and ensure your build configuration supports ESM.