Description
react-oidc-context is a lightweight authentication library for React single-page applications. It handles OpenID Connect and OAuth 2 sign-in using the oidc-client-ts library and React's context API for state management. You wrap your application in an AuthProvider, configured with your OpenID Connect settings, and the provider holds the user and the user manager and intercepts the redirect parameters after sign-in. Components then call the useAuth hook to read the loading state, whether the user is authenticated and the user object, and to call sign-in, remove-user and sign-out methods. For class components there is a withAuth higher-order component that adds an auth property. Protected routes can be wrapped with withAuthenticationRequired, which sends anonymous visitors to the login page. A callback in the configuration removes the login payload from the URL after a successful sign-in, which the README says is needed so that silent renewal keeps working on refresh. Access tokens can be renewed silently through oidc-client-ts. Other documented patterns include calling a protected API with the access token, adding event listeners through the hook, and automatic sign-in after the tab is reopened. The library is installed with npm or yarn, and it is aimed at teams building single-page apps that need login through an identity provider.…
Highlights
- OpenID Connect and OAuth 2 for React apps
- AuthProvider and useAuth hook
- withAuth and withAuthenticationRequired helpers
- Silent token renewal through oidc-client-ts
- Automatic sign-in on return visits
Ratings & Reviews
No ratings yet
Be the first to rate react-oidc-context.
People also visit
react-oidc-context FAQ
What does react-oidc-context do?
It adds OpenID Connect and OAuth 2 sign-in to a React single-page app, with the auth state shared through React context.
What does it build on?
The oidc-client-ts library, which its configuration is tightly coupled to.
How do I protect a route?
Wrap the route component with withAuthenticationRequired. Anonymous users are redirected to the login page.
Does it work with class components?
Yes. The withAuth higher-order component adds the auth property to class components.
How do I install it?
Run npm install react-oidc-context or use yarn, as shown in the README.
What are the best react-oidc-context alternatives?
Popular alternatives to react-oidc-context include redux-oidc, react-simple-oauth2-login and @react-oauth/google. See all react-oidc-context alternatives










