Use AI to integrate Auth0
Use AI to integrate Auth0
@auth0/auth0-react-router, configure the provider, and set up your routes. Full agent skills documentation →Get Started
This quickstart demonstrates how to add Auth0 authentication to a React Router application. You’ll build a secure app with login, logout, and user profile features using the Auth0 React Router SDK. The SDK handles the OIDC flow server-side and stores the session in a JWE-encrypted cookie — tokens never reach the browser.Create a new React Router project
Install the Auth0 React Router SDK
Configure Auth0
- Quick Setup
- CLI
- Dashboard
Configure environment variables
.env file at the root of your project:Create the Auth0 server instance
app/auth0.server.ts. The .server.ts suffix tells React Router’s bundler to exclude this file from the client bundle, keeping your secrets server-only.Add the auth routes
/auth/* paths. handleAuth dispatches internally to handleLogin, handleCallback, handleLogout, and handleBackchannelLogout based on the URL path and HTTP method.Configure the root layout
Auth0Provider and rootAuthLoader to app/root.tsx. rootAuthLoader decrypts the session cookie and passes the auth state to the provider — no tokens are sent to the browser.Auth0Provider reads session data from useRouteLoaderData('root'), so the root route must have the id root. With file-based routing React Router sets this from the filename automatically. With a custom route config, pass { id: 'root' } to the layout() call.Add login and logout
LoginButton redirects to /auth/login and LogoutButton redirects to /auth/logout. Auth0 handles the OIDC flow and redirects the user back to your app after sign-in.Show the user profile
useUser hook to access the authenticated user’s profile in any client component. Pair it with requireSession in the loader to block unauthenticated requests at the server before the page renders.Run your application
http://localhost:5173 in your browser and click Log in. You will be redirected to the Auth0 Universal Login page. After signing in you will be redirected back to your app.Troubleshooting
Callback URL mismatch — Auth0 returns an error after login
Callback URL mismatch — Auth0 returns an error after login
http://localhost:5173/auth/callback. Remove any trailing slashes or extra whitespace, then click Save Changes.404 on /auth/login — route not found
404 on /auth/login — route not found
auth.$.tsx splat route is missing or not registered in routes.ts.Fix: Confirm app/routes/auth.$.tsx exists and that app/routes.ts includes route('auth/*', 'routes/auth.$.tsx'). Restart the dev server after editing routes.ts.useUser returns null after login
useUser returns null after login
rootAuthLoader is not exported from app/root.tsx, or the root route does not have the id root.Fix: Confirm app/root.tsx exports export const loader = ({ request }) => rootAuthLoader(request). With a custom route config, register the root layout as layout('root.tsx', { id: 'root' }, [...routes]).TypeError on context.get — middleware not working
TypeError on context.get — middleware not working
defineRouteAuth and auth0Middleware require React Router 7.9.0 or later, which introduced the middleware API.Fix: Upgrade react-router to >=7.9.0, or protect routes individually using requireSession / requireUser in each loader instead.Advanced Usage
Call a backend API with an access token
Call a backend API with an access token
AUTH0_AUDIENCE to .env with your API’s identifier (from Auth0 Dashboard → Applications > APIs → API Settings → Identifier). Then use getAccessToken in a loader — the token never reaches the browser:Role-based route protection
Role-based route protection
defineRouteAuth middleware (React Router ≥ 7.9.0) to enforce roles at the route level. Roles are read from the https://auth0.com/claims/roles claim by default:403.SPA mode (client-side PKCE)
SPA mode (client-side PKCE)
@auth0/auth0-spa-js. Add VITE_AUTH0_DOMAIN and VITE_AUTH0_CLIENT_ID to your .env — Auth0Provider detects these automatically and switches to the PKCE flow. No other code changes are required.