Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@auth0/auth0-react

auth02.3mMIT2.3.0TypeScript support: included

Auth0 SDK for React Single Page Applications (SPA)

auth0, login, Authorization Code Grant Flow, PKCE, Single Page Application authentication, SPA authentication, react

readme

Auth0 SDK for React Single Page Applications

npm codecov Downloads License CircleCI

📚 Documentation - 🚀 Getting Started - 💻 API Reference - 💬 Feedback

Documentation

  • Quickstart - our interactive guide for quickly adding login, logout and user information to a React app using Auth0.
  • Sample App - a full-fledged React application integrated with Auth0.
  • FAQs - frequently asked questions about the auth0-react SDK.
  • Examples - code samples for common React authentication scenario's.
  • Docs site - explore our docs site and learn more about Auth0.

Getting started

Installation

Using npm

npm install @auth0/auth0-react

Using yarn

yarn add @auth0/auth0-react

Configure Auth0

Create a Single Page Application in the Auth0 Dashboard.

If you're using an existing application, verify that you have configured the following settings in your Single Page Application:

  • Click on the "Settings" tab of your application's page.
  • Scroll down and click on the "Show Advanced Settings" link.
  • Under "Advanced Settings", click on the "OAuth" tab.
  • Ensure that "JsonWebToken Signature Algorithm" is set to RS256 and that "OIDC Conformant" is enabled.

Next, configure the following URLs for your application under the "Application URIs" section of the "Settings" page:

  • Allowed Callback URLs: http://localhost:3000
  • Allowed Logout URLs: http://localhost:3000
  • Allowed Web Origins: http://localhost:3000

These URLs should reflect the origins that your application is running on. Allowed Callback URLs may also include a path, depending on where you're handling the callback.

Take note of the Client ID and Domain values under the "Basic Information" section. You'll need these values in the next step.

Configure the SDK

Configure the SDK by wrapping your application in Auth0Provider:

// src/index.js
import React from 'react';
import { createRoot } from 'react-dom/client';
import { Auth0Provider } from '@auth0/auth0-react';
import App from './App';

const root = createRoot(document.getElementById('app'));

root.render(
  <Auth0Provider
    domain="YOUR_AUTH0_DOMAIN"
    clientId="YOUR_AUTH0_CLIENT_ID"
    authorizationParams={{
      redirect_uri: window.location.origin,
    }}
  >
    <App />
  </Auth0Provider>
);
<summary>Instructions for React <18</summary>
// src/index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { Auth0Provider } from '@auth0/auth0-react';
import App from './App';

ReactDOM.render(
  <Auth0Provider
    domain="YOUR_AUTH0_DOMAIN"
    clientId="YOUR_AUTH0_CLIENT_ID"
    authorizationParams={{
      redirect_uri: window.location.origin,
    }}
  >
    <App />
  </Auth0Provider>,
  document.getElementById('app')
);

Use the useAuth0 hook in your components to access authentication state (isLoading, isAuthenticated and user) and authentication methods (loginWithRedirect and logout):

// src/App.js
import React from 'react';
import { useAuth0 } from '@auth0/auth0-react';

function App() {
  const { isLoading, isAuthenticated, error, user, loginWithRedirect, logout } =
    useAuth0();

  if (isLoading) {
    return <div>Loading...</div>;
  }
  if (error) {
    return <div>Oops... {error.message}</div>;
  }

  if (isAuthenticated) {
    return (
      <div>
        Hello {user.name}{' '}
        <button onClick={() => logout({ logoutParams: { returnTo: window.location.origin } })}>
          Log out
        </button>
      </div>
    );
  } else {
    return <button onClick={() => loginWithRedirect()}>Log in</button>;
  }
}

export default App;

For more code samples on how to integrate auth0-react SDK in your React application, have a look at our examples.

API reference

Explore public API's available in auth0-react.

Feedback

Contributing

We appreciate feedback and contribution to this repo! Before you get started, please see the following:

Raise an issue

To provide feedback or report a bug, please raise an issue on our issue tracker.

Vulnerability Reporting

Please do not report security vulnerabilities on the public GitHub issue tracker. The Responsible Disclosure Program details the procedure for disclosing security issues.


<picture> <source media="(prefers-color-scheme: light)" srcset="https://cdn.auth0.com/website/sdks/logos/auth0_light_mode.png" width="150"> <source media="(prefers-color-scheme: dark)" srcset="https://cdn.auth0.com/website/sdks/logos/auth0_dark_mode.png" width="150"> Auth0 Logo </picture>

Auth0 is an easy to implement, adaptable authentication and authorization platform. To learn more checkout Why Auth0?

This project is licensed under the MIT license. See the LICENSE file for more info.

changelog

Change Log

v2.3.0 (2025-01-21)

Full Changelog

Added

Changed

Fixed

Removed

Security

v2.2.4 (2023-12-11)

Full Changelog

Changed

v2.2.3 (2023-11-02)

Full Changelog

Fixed

v2.2.2 (2023-10-31)

Full Changelog

Fixed

Changed

v2.2.1 (2023-08-22)

Full Changelog

Fixed

v2.2.0 (2023-07-13)

Full Changelog

Added

Fixed

v2.1.1 (2023-06-14)

Full Changelog

Changed

v2.1.0 (2023-05-05)

Full Changelog

Added

  • Adding onBeforeAuthentication to the withAuthenticationRequired HOC #534 (stephenkelzer)

v2.0.2 (2023-04-26)

Full Changelog

Fixed

  • Remove useUnknownInCatchVariables to be compliant with TS ^4.4 #511 (cemercier)

v2.0.1 (2023-02-22)

Full Changelog

Fixed

v2.0.0 (2023-01-19)

Auth0-React v2 includes many significant changes compared to v1:

  • Removal of polyfills from bundles
  • Introduction of authorizationParams and logoutParams for properties sent to Auth0
  • Removal of buildAuthorizeUrl and buildLogoutUrl
  • Removal of redirectMethod on loginWithRedirect in favour of openUrl
  • Removal of localOnly from logout in favour of openUrl
  • Renaming of ignoreCache to cacheMode and introduction of cache-only
  • Use application/x-www-form-urlencoded by default
  • Do not fallback to refreshing tokens via iframe by default
  • Changes to default scopes and removal of advancedOptions.defaultScope
  • Removal of claimCheck on withAuthenticationRequired

As with any major version bump, v2 of Auth0-React contains a set of breaking changes. Please review the migration guide thoroughly to understand the changes required to migrate your application to v2.

v2.0.0-beta.0 (2022-12-12)

Auth0-React v2 includes many significant changes compared to v1:

  • Removal of polyfills from bundles
  • Introduction of authorizationParams and logoutParams for properties sent to Auth0
  • Removal of buildAuthorizeUrl and buildLogoutUrl
  • Removal of redirectMethod on loginWithRedirect in favour of openUrl
  • Removal of localOnly from logout in favour of openUrl
  • Renaming of ignoreCache to cacheMode and introduction of cache-only
  • Use application/x-www-form-urlencoded by default
  • Do not fallback to refreshing tokens via iframe by default
  • Changes to default scopes and removal of advancedOptions.defaultScope
  • Removal of claimCheck on withAuthenticationRequired

As with any major version bump, v2 of Auth0-React contains a set of breaking changes. Please review the migration guide thoroughly to understand the changes required to migrate your application to v2.

v1.12.1 (2023-01-12)

Full Changelog

Security

This patch release is identical to 1.12.0 but has been released to ensure tooling no longer detects a vulnerable version of jsonwebtoken being used by @auth0/auth0-spa-js.

Even though 1.22.5 of @auth0/auth0-spa-js was not vulnerable for the related CVE because of the fact that jsonwebtoken is a devDependency of @auth0/auth0-spa-js, we are cutting a release to ensure build tools no longer report our SDK's that use @auth0/auth0-spa-js as vulnerable to the mentioned CVE.

v1.12.0 (2022-10-12)

Full Changelog

Added

v1.11.0 (2022-09-13)

Full Changelog

Added

v1.10.2 (2022-06-22)

Full Changelog

Fixed

v1.10.1 (2022-04-28)

Full Changelog

Fixed

  • Make sure handleRedirectCallback is only called once in StrictMode React 18 #355 (adamjmcgrath)

v1.10.0 (2022-04-19)

Full Changelog

Added

Fixed

v1.9.0 (2022-01-14)

Full Changelog

Added

Fixed

v1.8.0 (2021-09-20)

Full Changelog

Added

v1.7.0 (2021-09-06)

Full Changelog

Added

v1.6.0 (2021-07-14)

Added

v1.5.0 (2021-05-05)

Added

Changed

Fixed

v1.4.0 (2021-03-26)

Added

Fixed

v1.3.0 (2021-02-16)

Added

  • Added buildAuthorizeUrl and buildLogoutUrl #190 (THISS)

Changed

v1.2.0 (2020-11-04)

Added

v1.1.0 (2020-09-17)

Added

Fixed

v1.0.0 (2020-06-19)

Breaking Change

v0.4.0 (2020-06-05)

Added

v0.3.1 (2020-06-01)

Fixed

v0.3.0 (2020-05-29)

Added

Breaking Changes

v0.2.0 (2020-05-20)

Added

  • [SDK-1642] Add missing methods from SPA JS #11 (adamjmcgrath)
  • [SDK-1582] Normalize the auth0 error and add error handling to the basic example #10 (adamjmcgrath)

Changed

v0.1.0 (2020-05-08)

Added