Auth0 Authentication Provider for react-admin
ra-auth-auth0 is an authentication provider package designed to integrate Auth0 authentication seamlessly within applications built with the react-admin framework. The current stable version is 2.0.1, which supports react-admin v5 and @auth0/auth0-spa-js v2.x. This library typically releases updates in alignment with major react-admin versions to maintain compatibility, alongside patch releases for bug fixes. Its key differentiators include providing a pre-built Auth0AuthProvider that handles the authentication flow, an httpClient utility to forward Auth0 tokens to backend data providers, and flexible mechanisms for handling user permissions and roles via Auth0 claims, with options to customize redirect URIs and checkAuth behavior. It simplifies the integration of enterprise-grade authentication into react-admin applications.
Common errors
-
Error: Invalid state
cause Using `ra-auth-auth0` versions prior to 2.0.1 in React's Strict Mode.fixUpgrade `ra-auth-auth0` to version 2.0.1 or newer. -
TypeError: Cannot read properties of undefined (reading 'push')
cause The `Auth0AuthProvider` relies on react-router's navigation context, which is missing if `BrowserRouter` is not wrapping the application.fixWrap your `Admin` component, or your entire application, with `<BrowserRouter>` from `react-router-dom`. -
Infinite redirect loop on failures
cause Misconfiguration of redirect URIs or `checkAuth` logic, potentially related to `redirectOnCheckAuth` setting.fixVerify `loginRedirectUri` and `logoutRedirectUri` are correctly set. Ensure `redirectOnCheckAuth` is configured appropriately for your login flow; setting it to `false` requires a custom login page. -
checkAuth should reject when redirectOnCheckAuth is set to false
cause Older versions of the provider might not correctly reject `checkAuth` when `redirectOnCheckAuth` is explicitly `false`, leading to unexpected behavior with custom login pages.fixUpgrade `ra-auth-auth0` to version 1.1.0 or newer to ensure correct `checkAuth` rejection behavior with `redirectOnCheckAuth: false`.
Warnings
- breaking Version 2.0.0 introduced breaking changes by upgrading peer dependencies to `react-admin` v5. Applications using older `react-admin` versions should stick to `ra-auth-auth0` v1.x.
- breaking In version 2.0.0, internal `react-admin` imports changed from `ra-core` to `react-admin`. If you previously had custom code relying on internal imports, adjust them accordingly.
- gotcha The `Auth0AuthProvider` requires the application to be wrapped within a `BrowserRouter` component from `react-router-dom` to handle redirects and routing correctly. Missing this will lead to authentication failures.
- gotcha If using React's Strict Mode (e.g., in development), versions prior to 2.0.1 could encounter an 'Invalid state' error during the authentication flow.
- gotcha To retrieve user roles or custom permissions from Auth0, you must configure Auth0 Actions to include these claims in the authentication token. By default, `ra-auth-auth0` looks for a claim with 'role' in its name, which might not match your Auth0 setup.
Install
-
npm install ra-auth-auth0 -
yarn add ra-auth-auth0 -
pnpm add ra-auth-auth0
Imports
- Auth0AuthProvider
const Auth0AuthProvider = require('ra-auth-auth0').Auth0AuthProvider;import { Auth0AuthProvider } from 'ra-auth-auth0'; - httpClient
import httpClient from 'ra-auth-auth0/httpClient';
import { httpClient } from 'ra-auth-auth0'; - Auth0Client
import { Auth0Client } from 'ra-auth-auth0';import { Auth0Client } from '@auth0/auth0-spa-js';
Quickstart
import React, { useEffect, useRef, useState } from 'react';
import { Admin, Resource } from 'react-admin';
import { BrowserRouter } from 'react-router-dom';
import { Auth0AuthProvider, httpClient } from 'ra-auth-auth0';
import { Auth0Client } from '@auth0/auth0-spa-js';
import jsonServerProvider from 'ra-data-json-server';
// Environment variables for Auth0 configuration (replace with your actual values or .env)
const VITE_AUTH0_DOMAIN = process.env.VITE_AUTH0_DOMAIN ?? 'your-auth0-domain.auth0.com';
const VITE_AUTH0_CLIENT_ID = process.env.VITE_AUTH0_CLIENT_ID ?? 'your-client-id';
const VITE_AUTH0_AUDIENCE = process.env.VITE_AUTH0_AUDIENCE ?? 'your-api-audience';
const VITE_LOGIN_REDIRECT_URL = process.env.VITE_LOGIN_REDIRECT_URL ?? `${window.location.origin}/auth-callback`;
const VITE_LOGOUT_REDIRECT_URL = process.env.VITE_LOGOUT_REDIRECT_URL ?? window.location.origin;
const auth0Client = new Auth0Client({
domain: VITE_AUTH0_DOMAIN,
clientId: VITE_AUTH0_CLIENT_ID,
cacheLocation: 'localstorage',
authorizationParams: {
audience: VITE_AUTH0_AUDIENCE,
redirect_uri: VITE_LOGIN_REDIRECT_URL,
},
});
const authProvider = Auth0AuthProvider(auth0Client, {
loginRedirectUri: VITE_LOGIN_REDIRECT_URL,
logoutRedirectUri: VITE_LOGOUT_REDIRECT_URL,
});
// Example dataProvider using httpClient to pass Auth0 token
const dataProvider = jsonServerProvider(
'http://localhost:3000',
httpClient(auth0Client)
);
const App = () => {
return (
<BrowserRouter>
<Admin authProvider={authProvider} dataProvider={dataProvider}>
<Resource name="posts" />
</Admin>
</BrowserRouter>
);
};
export default App;