React OIDC Context

3.3.1 · active · verified Sun Apr 19

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.

Common errors

Warnings

Install

Imports

Quickstart

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.

import React from 'react';
import ReactDOM from 'react-dom/client';
import { AuthProvider, useAuth } from 'react-oidc-context';

// Your OIDC configuration
const oidcConfig = {
  authority: 'https://your-identity-provider.com',
  client_id: 'your-client-id',
  redirect_uri: window.location.origin + '/authentication/callback',
  onSigninCallback: () => {
    // IMPORTANT: Clear URL payload upon successful login to prevent issues with silent renew
    window.history.replaceState({}, document.title, window.location.pathname);
  },
  // automaticSilentRenew: true, // Enable automatic token renewal
  // scope: 'openid profile email',
  // ... other oidc-client-ts UserManagerSettings
};

function App() {
    const auth = useAuth();

    switch (auth.activeNavigator) {
        case 'signinSilent':
            return <div>Signing you in silently...</div>;
        case 'signoutRedirect':
            return <div>Signing you out...</div>;
    }

    if (auth.isLoading) {
        return <div>Loading authentication status...</div>;
    }

    if (auth.error) {
        return <div>Authentication error: {auth.error.message}</div>;
    }

    if (auth.isAuthenticated) {
        return (
        <div>
            Hello, {auth.user?.profile.name || auth.user?.profile.sub}{' '}
            <button onClick={() => void auth.removeUser()}>Log out locally</button>
            <button onClick={() => void auth.signoutRedirect()}>Log out from IdP</button>
        </div>
        );
    }

    return (
      <div>
        <p>You are not authenticated.</p>
        <button onClick={() => void auth.signinRedirect()}>Log in</button>
      </div>
    );
}

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
    <AuthProvider {...oidcConfig}>
      <App />
    </AuthProvider>
  </React.StrictMode>
);

view raw JSON →