Skip to main content
Auth0 React SDK for Single Page Apps
Describes the Auth0 SDK for React Single Page Apps
The Auth0 React SDK (auth0-react.js) is a JavaScript library for implementing authentication and authorization in React apps with Auth0. It provides a custom React hook and other Higher Order Components so you can secure React apps using best practices while writing less code.
The Auth0 React SDK handles grant and protocol details, token expiration and renewal, as well as token storage and caching. Under the hood, it implements Universal Login and the Authorization Code Grant Flow with PKCE.
The library is hosted on GitHub where you can read more about the API.
Note If you are using a custom router, you will need to supply the
Installation
You have a few options for using auth0-react.js in your project.Getting started
First, you’ll need to wrap your application in a singleAuth0Provider component. This will provide the React Context to components that are placed inside your application.
isLoading and error
Wait for the SDK to initialize and handle any errors with theisLoading and error states.
Login
UseloginWithRedirect or loginWithPopup to log your users in.
Logout
Uselogout to log your users out. Make sure returnTo is specified in “Allowed Logout URLs” in your .
User
Access user profile information with theuser value.
Use with a class component
Use thewithAuth0 Higher Order Component to add the auth0 property to class components instead of using the hook.
Protect a route
Protect a route component using thewithAuthenticationRequired higher order component. Visits to this route when unauthenticated will redirect the user to the login page and back to this page after login.
Auth0Provider with a custom onRedirectCallback method to perform the action that returns the user to the protected page. See examples for react-router, Gatsby and Next.js.
Call an API
To call a protected API with an , be sure to specify theaudience and scope of your access token, either in Auth0Provider or getAccessTokenSilently. Then use it to call a protected API by passing it in the Authorization header of your request.
