Repo of the Day
auth0/auth0-react
Published: Nov 21, 2022
Open repository ↗Summary
auth0/auth0-react is the official Auth0 SDK for React Single Page Applications, distributed as @auth0/auth0-react on npm and licensed under MIT. It provides React components and hooks that wrap Auth0's authentication flows so engineers can add login, logout, and user profile handling without hand-rolling OAuth/OIDC logic. The SDK is written in TypeScript and is intended for SPAs running entirely in the browser.
What it is useful for
The SDK is useful when a React app needs delegated authentication through Auth0 and the engineer wants a React-idiomatic interface rather than direct calls to the underlying auth client. Concrete scenarios documented in the README include:
- Wiring a Single Page Application to Auth0 using the OIDC Authorization Code with PKCE flow (the README notes RS256 and OIDC Conformant must be enabled in the dashboard).
- Reading auth state in components through the
useAuth0hook, which exposesisLoading,isAuthenticated,error,user,loginWithRedirect, andlogout. - Protecting routes via the
withAuthenticationRequiredhigher-order component or theuseAuth0Suspensehook for React Suspense-based loading states. - Integrating with Auth0 enterprise connections using the
useEnterpriseConnecthook. - Enabling Anonymous Sessions, which assign a persistent identity to visitors before login (Early Access; requires contacting an Auth0 representative).
- Forcing a specific Experiment Center variant on login by passing
experiment_idandvariation_idinauthorizationParams(Early Access).
Documented limitations include the Early Access status of both Anonymous Sessions and Experiment Center support, which are not generally available without approval.
How engineers can use it
Setup steps shown in the README:
- Install the package:
ornpm install @auth0/auth0-reactyarn add @auth0/auth0-react. - Create a Single Page Application in the Auth0 Dashboard and set Allowed Callback URLs, Allowed Logout URLs, and Allowed Web Origins to the app's origin (the README example uses
http://localhost:3000). - Wrap the app in
Auth0Providerwith the domain, clientId, and aredirect_uriinauthorizationParams. - Use
useAuth0()in components to triggerloginWithRedirect()andlogout({ logoutParams: { returnTo: window.location.origin } })and to render the user object.
For full configuration, additional examples (e.g., silent authentication, organization handling), and the public API surface, the README points to the linked EXAMPLES.md, FAQ.md, and the generated API reference at auth0.github.io/auth0-react.