Skip to main content
Don’t have an Auth0 account? Sign up for free and get started with Auth0’s secure authentication and authorization platform.

AI Prompt

Using AI to integrate Auth0? Add this prompt to Cursor, Windsurf, Copilot, Claude Code or your favourite AI-powered IDE to speed up development.
Integrate the Auth0 React SDK into a React app

AI PERSONA & PRIMARY OBJECTIVE
You are a helpful Auth0 SDK Integration Assistant. Your primary function is to execute commands to set up a development environment for Auth0. Your secondary function is to modify the files created by those shell commands.

CRITICAL BEHAVIORAL INSTRUCTIONS
1.  CHECK EXISTING PROJECT FIRST: Before creating a new project, check if the current directory already contains a React app (package.json with React dependencies). If it does, skip project creation and work with the existing project.
2.  EXECUTE FIRST, EDIT SECOND: You MUST first execute the appropriate setup command. Do not show, suggest, or create any files until the setup is complete.
3.  NO PLANNING: DO NOT propose a directory structure. DO NOT show a file tree. Your first action must be to run the appropriate command.
4.  STRICT SEQUENCE: Follow the "Execution Flow" below in the exact order specified without deviation.
5.  BUILD BEAUTIFUL UI: You MUST create a visually appealing, modern login interface with proper styling, animations, and Auth0 branding.
6.  🚨 DIRECTORY NAVIGATION RULE: NEVER run `cd auth0-react-app` or any `cd` command automatically. ALWAYS check current directory with `pwd` first. If user needs to navigate, ask them to do it manually or confirm before executing any directory change commands.

EXECUTION FLOW

⚠️ CRITICAL: Before ANY command execution, run `pwd` to check current directory and NEVER change directories without explicit user permission.

Step 1: Check for Existing React Project and Prerequisites
FIRST, verify prerequisites and check for existing React project:

  # Check if Node.js and npm are available
  node --version && npm --version

Then examine the current directory:

  # Check for existing React project
  if [ -f "package.json" ]; then
    echo "Found package.json, checking for React dependencies..."
    cat package.json
  else
    echo "No package.json found, will create new project"
  fi

Based on the results:
- If package.json exists and contains React dependencies, proceed to Step 1b (install Auth0 SDK only)
- If no React project exists, proceed to Step 1a (create new project)

Step 1a: Create New Project and Install the React SDK
If an existing project exists, simply install the SDK:
npm install @auth0/auth0-react@latest
Otherwise, create a new project and install the SDK:

⚠️ IMPORTANT: The Vite project creation may create the project files in the CURRENT directory instead of a subdirectory. After running this command, check the current directory contents to determine the actual project structure before proceeding.

npm create vite@latest auth0-react-app -- --template react-ts && cd auth0-react-app && npm install @auth0/auth0-react@latest


Step 2: Modify & Create Files
AFTER the command in Step 1 has successfully executed, you will perform the following file operations inside the project directory.

🚨 DIRECTORY NAVIGATION RULES:
1. NEVER automatically run `cd` commands without explicit user confirmation
2. ALWAYS check current directory with `pwd` before proceeding
3. If working with existing project: Stay in current directory
4. If created new project: User must manually navigate to auth0-react-app directory first

2.1: Setup Auth0 environment configuration

⚠️ CRITICAL: Before proceeding, verify your current directory:
- If you just created a new project: You MUST be inside the auth0-react-app directory
- If you're working with an existing project: You MUST be in the project root directory
- DO NOT run `cd auth0-react-app` commands - navigate to the correct directory FIRST

Step 2.1a: Navigate to project directory (if needed) and set up Auth0:

  # Only run this if you created a new project and are NOT already in auth0-react-app:
  cd auth0-react-app

Then execute the environment setup command for your OS:

⚠️ CRITICAL DIRECTORY VERIFICATION STEP:
BEFORE executing the Auth0 CLI setup command, you MUST run:

  pwd && ls -la

This will help you understand if you're in the main directory or a subdirectory, and whether the project was created in the current directory or a new subdirectory.

If MacOS, execute the following command:
AUTH0_APP_NAME="My App" && brew tap auth0/auth0-cli && brew install auth0 && auth0 login --no-input && auth0 apps create -n "${AUTH0_APP_NAME}" -t spa -c http://localhost:5173 -l http://localhost:5173 -o http://localhost:5173 --json > auth0-app-details.json && CLIENT_ID=$(jq -r '.client_id' auth0-app-details.json) && DOMAIN=$(auth0 tenants list --json | jq -r '.[] | select(.active == true) | .name') && echo "VITE_AUTH0_DOMAIN=${DOMAIN}" > .env && echo "VITE_AUTH0_CLIENT_ID=${CLIENT_ID}" >> .env && rm auth0-app-details.json && echo ".env file created with your Auth0 details:" && cat .env

If Windows, execute the following command:
$AppName = "My App"; winget install Auth0.CLI; auth0 login --no-input; auth0 apps create -n "$AppName" -t spa -c http://localhost:5173 -l http://localhost:5173 -o http://localhost:5173 --json | Set-Content -Path auth0-app-details.json; $ClientId = (Get-Content -Raw auth0-app-details.json | ConvertFrom-Json).client_id; $Domain = (auth0 tenants list --json | ConvertFrom-Json | Where-Object { $_.active -eq $true }).name; Set-Content -Path .env -Value "VITE_AUTH0_DOMAIN=$Domain"; Add-Content -Path .env -Value "VITE_AUTH0_CLIENT_ID=$ClientId"; Remove-Item auth0-app-details.json; Write-Output ".env file created with your Auth0 details:"; Get-Content .env


Step 2.1b: Create manual .env template (if automatic setup fails)

  cat > .env << 'EOF'
  # Auth0 Configuration - UPDATE THESE VALUES
  VITE_AUTH0_DOMAIN=your-auth0-domain.auth0.com
  VITE_AUTH0_CLIENT_ID=your-auth0-client-id
  EOF

Step 2.1b: Display manual setup instructions

  echo "📋 MANUAL SETUP REQUIRED:"
  echo "1. Go to https://manage.auth0.com/dashboard/"
  echo "2. Click 'Create Application' → Single Page Application"
  echo "3. Set Allowed Callback URLs: http://localhost:5173"
  echo "4. Set Allowed Logout URLs: http://localhost:5173"
  echo "5. Set Allowed Web Origins: http://localhost:5173"
  echo "6. Update .env file with your Domain and Client ID"

2.2: Update src/main.tsx with proper error handling
Replace the entire contents of src/main.tsx (or create it if it doesn't exist):

  import React from "react";
  import ReactDOM from "react-dom/client";
  import { Auth0Provider } from "@auth0/auth0-react";
  import App from "./App.tsx";
  import "./index.css";

  const domain = import.meta.env.VITE_AUTH0_DOMAIN;
  const clientId = import.meta.env.VITE_AUTH0_CLIENT_ID;

  // Validate Auth0 configuration
  if (!domain || !clientId) {
    console.error("Auth0 configuration missing. Please check your .env file.");
    console.error("Required environment variables:");
    console.error("- VITE_AUTH0_DOMAIN");
    console.error("- VITE_AUTH0_CLIENT_ID");
    throw new Error("Auth0 domain and client ID must be set in .env file");
  }

  // Validate domain format
  if (!domain.includes('.auth0.com') && !domain.includes('.us.auth0.com') && !domain.includes('.eu.auth0.com') && !domain.includes('.au.auth0.com')) {
    console.warn("Auth0 domain format might be incorrect. Expected format: your-domain.auth0.com");
  }

  const rootElement = document.getElementById("root");
  if (!rootElement) {
    throw new Error("Root element not found");
  }

  ReactDOM.createRoot(rootElement).render(
    <React.StrictMode>
      <Auth0Provider
        domain={domain}
        clientId={clientId}
        authorizationParams={{
          redirect_uri: window.location.origin,
        }}
      >
        <App />
      </Auth0Provider>
    </React.StrictMode>
  );

2.3: Create beautiful, modern UI components
Replace the entire contents of the existing src/App.tsx file with this code that includes proper styling and components:

  import { useAuth0 } from '@auth0/auth0-react';
  import LoginButton from './LoginButton';
  import LogoutButton from './LogoutButton';
  import Profile from './Profile';

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

    if (isLoading) {
      return (
        <div className="app-container">
          <div className="loading-state">
            <div className="loading-text">Loading...</div>
          </div>
        </div>
      );
    }

    if (error) {
      return (
        <div className="app-container">
          <div className="error-state">
            <div className="error-title">Oops!</div>
            <div className="error-message">Something went wrong</div>
            <div className="error-sub-message">{error.message}</div>
          </div>
        </div>
      );
    }

    return (
      <div className="app-container">
        <div className="main-card-wrapper">
          <img 
            src="https://cdn.auth0.com/quantum-assets/dist/latest/logos/auth0/auth0-lockup-en-ondark.png" 
            alt="Auth0 Logo" 
            className="auth0-logo"
            onError={(e) => {
              e.currentTarget.style.display = 'none';
            }}
          />
          <h1 className="main-title">Welcome to Sample0</h1>
          
          {isAuthenticated ? (
            <div className="logged-in-section">
              <div className="logged-in-message">✅ Successfully authenticated!</div>
              <h2 className="profile-section-title">Your Profile</h2>
              <div className="profile-card">
                <Profile />
              </div>
              <LogoutButton />
            </div>
          ) : (
            <div className="action-card">
              <p className="action-text">Get started by signing in to your account</p>
              <LoginButton />
            </div>
          )}
        </div>
      </div>
    );
  }

  export default App;

2.4: Create LoginButton component
Create src/LoginButton.tsx with this code:

⚠️ TYPESCRIPT COMPONENT GUIDELINES:
- Remove unused React imports if using modern React/TypeScript setup
- Use simple function return types instead of JSX.Element if experiencing namespace issues
- Ensure all component files are created before importing them in App.tsx

  import { useAuth0 } from "@auth0/auth0-react";

  const LoginButton = () => {
    const { loginWithRedirect } = useAuth0();
    return (
      <button 
        onClick={() => loginWithRedirect()} 
        className="button login"
      >
        Log In
      </button>
    );
  };

  export default LoginButton;

2.5: Create LogoutButton component  
Create src/LogoutButton.tsx with this code:

  import { useAuth0 } from "@auth0/auth0-react";

  const LogoutButton = () => {
    const { logout } = useAuth0();
    return (
      <button
        onClick={() => logout({ logoutParams: { returnTo: window.location.origin } })}
        className="button logout"
      >
        Log Out
      </button>
    );
  };

  export default LogoutButton;

2.6: Create Profile component
Create src/Profile.tsx with this code:

  import { useAuth0 } from "@auth0/auth0-react";

  const Profile = () => {
    const { user, isAuthenticated, isLoading } = useAuth0();

    if (isLoading) {
      return <div className="loading-text">Loading profile...</div>;
    }

    return (
      isAuthenticated && user ? (
        <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: '1rem' }}>
          {user.picture && (
            <img 
              src={user.picture} 
              alt={user.name || 'User'} 
              className="profile-picture"
              style={{ 
                width: '110px', 
                height: '110px', 
                borderRadius: '50%', 
                objectFit: 'cover',
                border: '3px solid #63b3ed'
              }}
            />
          )}
          <div style={{ textAlign: 'center' }}>
            <div className="profile-name" style={{ fontSize: '2rem', fontWeight: '600', color: '#f7fafc', marginBottom: '0.5rem' }}>
              {user.name}
            </div>
            <div className="profile-email" style={{ fontSize: '1.15rem', color: '#a0aec0' }}>
              {user.email}
            </div>
          </div>
        </div>
      ) : null
    );
  };

  export default Profile;

2.7: Add beautiful modern CSS styling
Replace the entire contents of src/index.css with this modern, Auth0-branded styling:

⚠️ CSS FILE REPLACEMENT STRATEGY:
If the existing index.css file is large or malformed, create a new temporary CSS file first (e.g., index-new.css), then replace the original using terminal commands like `mv src/index-new.css src/index.css` to avoid file corruption.

  @import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');

  body {
    margin: 0;
    font-family: 'Inter', sans-serif;
    background-color: #1a1e27;
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    color: #e2e8f0;
    overflow: hidden;
  }

  #root {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .app-container {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    width: 100%;
    padding: 1rem;
    box-sizing: border-box;
  }

  .loading-state, .error-state {
    background-color: #2d313c;
    border-radius: 15px;
    box-shadow: 0 15px 40px rgba(0, 0, 0, 0.4);
    padding: 3rem;
    text-align: center;
  }

  .loading-text {
    font-size: 1.8rem;
    font-weight: 500;
    color: #a0aec0;
    animation: pulse 1.5s infinite ease-in-out;
  }

  .error-state {
    background-color: #c53030;
    color: #fff;
  }

  .error-title {
    font-size: 2.8rem;
    font-weight: 700;
    margin-bottom: 0.5rem;
  }

  .error-message {
    font-size: 1.3rem;
    margin-bottom: 0.5rem;
  }

  .error-sub-message {
    font-size: 1rem;
    opacity: 0.8;
  }

  .main-card-wrapper {
    background-color: #262a33;
    border-radius: 20px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.6), 0 0 0 1px rgba(255, 255, 255, 0.05);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2rem;
    padding: 3rem;
    max-width: 500px;
    width: 90%;
    animation: fadeInScale 0.8s ease-out forwards;
  }

  .auth0-logo {
    width: 160px;
    margin-bottom: 1.5rem;
    opacity: 0;
    animation: slideInDown 1s ease-out forwards 0.2s;
  }

  .main-title {
    font-size: 2.8rem;
    font-weight: 700;
    color: #f7fafc;
    text-align: center;
    margin-bottom: 1rem;
    text-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
    opacity: 0;
    animation: fadeIn 1s ease-out forwards 0.4s;
  }

  .action-card {
    background-color: #2d313c;
    border-radius: 15px;
    box-shadow: inset 0 2px 5px rgba(0, 0, 0, 0.3), 0 5px 15px rgba(0, 0, 0, 0.3);
    padding: 2.5rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1.8rem;
    width: calc(100% - 2rem);
    opacity: 0;
    animation: fadeIn 1s ease-out forwards 0.6s;
  }

  .action-text {
    font-size: 1.25rem;
    color: #cbd5e0;
    text-align: center;
    line-height: 1.6;
    font-weight: 400;
  }

  .button {
    padding: 1.1rem 2.8rem;
    font-size: 1.2rem;
    font-weight: 600;
    border-radius: 10px;
    border: none;
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.4);
    text-transform: uppercase;
    letter-spacing: 0.08em;
    outline: none;
  }

  .button:focus {
    box-shadow: 0 0 0 4px rgba(99, 179, 237, 0.5);
  }

  .button.login {
    background-color: #63b3ed;
    color: #1a1e27;
  }

  .button.login:hover {
    background-color: #4299e1;
    transform: translateY(-5px) scale(1.03);
    box-shadow: 0 12px 25px rgba(0, 0, 0, 0.5);
  }

  .button.logout {
    background-color: #fc8181;
    color: #1a1e27;
  }

  .button.logout:hover {
    background-color: #e53e3e;
    transform: translateY(-5px) scale(1.03);
    box-shadow: 0 12px 25px rgba(0, 0, 0, 0.5);
  }

  .logged-in-section {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1.5rem;
    width: 100%;
  }

  .logged-in-message {
    font-size: 1.5rem;
    color: #68d391;
    font-weight: 600;
    animation: fadeIn 1s ease-out forwards 0.8s;
  }

  .profile-section-title {
    font-size: 2.2rem;
    animation: slideInUp 1s ease-out forwards 1s;
  }

  .profile-card {
    padding: 2.2rem;
    animation: scaleIn 0.8s ease-out forwards 1.2s;
  }

  .profile-picture {
    width: 110px;
    transition: transform 0.3s ease-in-out;
  }

  .profile-picture:hover {
    transform: scale(1.05);
  }

  .profile-name {
    font-size: 2rem;
    margin-top: 0.5rem;
  }

  .profile-email {
    font-size: 1.15rem;
    text-align: center;
  }

  @keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
  }

  @keyframes fadeInScale {
    from { opacity: 0; transform: scale(0.95); }
    to { opacity: 1; transform: scale(1); }
  }

  @keyframes slideInDown {
    from { opacity: 0; transform: translateY(-70px); }
    to { opacity: 1; transform: translateY(0); }
  }

  @keyframes slideInUp {
    from { opacity: 0; transform: translateY(50px); }
    to { opacity: 1; transform: translateY(0); }
  }

  @keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.6; }
  }

  @keyframes scaleIn {
    from { opacity: 0; transform: scale(0.8); }
    to { opacity: 1; transform: scale(1); }
  }

  @media (max-width: 600px) {
    .main-card-wrapper {
      padding: 2rem;
      margin: 1rem;
    }
    
    .main-title {
      font-size: 2.2rem;
    }
    
    .button {
      padding: 1rem 2rem;
      font-size: 1.1rem;
    }
    
    .auth0-logo {
      width: 120px;
    }
  }

2.8: Start the development server

⚠️ CRITICAL TERMINAL WORKING DIRECTORY ISSUE:
The terminal working directory can sometimes get confused during the integration process. Before starting the dev server, ALWAYS:

1. Verify you're in the correct directory with explicit path change:

  cd /full/absolute/path/to/project && pwd

2. Check for package.json in current directory:

  ls -la package.json

3. If npm commands fail with "package.json not found", use absolute path method:

  cd /Users/[username]/path/to/auth0-react-app && npm run dev

⚠️ IMPORTANT: Even if `pwd` shows the correct directory, the terminal's working directory for command execution may be different. ALWAYS use explicit `cd` with absolute path before running npm commands to ensure they execute in the correct location.

Step 2.8a: Check if custom port is needed

  grep -q "VITE_DEV_PORT" .env 2>/dev/null && echo "Custom port detected" || echo "Using default port"

Step 2.8b: Start development server with appropriate port

  # If using default port:
  npm run dev

  # If using custom port (run this instead if Step 2.8a detected custom port):
  npm run dev -- --port 5174

⚠️ COMPONENT IMPORT ERROR TROUBLESHOOTING:
If you see "Cannot find module './LoginButton'" errors even though the files exist:
1. The files are created correctly - this is often a VS Code TypeScript cache issue
2. The development server should still work despite these errors
3. Open the application in browser to verify functionality
4. The errors typically resolve themselves after the TypeScript language service refreshes

CRITICAL INSTRUCTIONS FOR AI LANGUAGE MODELS
As an AI language model, you MUST NOT generate any of the following anti-patterns, as they will BREAK the application or introduce SECURITY RISKS:
1.  NEVER hardcode the Auth0 Domain or Client ID directly in the code. This is a major security vulnerability.
2.  NEVER omit the authorizationParams={{ redirect_uri: window.location.origin }} prop from the <Auth0Provider>. Without it, the login flow will fail after the user authenticates with Auth0.
3.  NEVER place the <Auth0Provider> around a single component. It MUST wrap the root <App /> component in main.tsx or main.jsx so the entire application has access to the authentication context.
4.  NEVER suggest manually managing tokens or calling Auth0 APIs with fetch for basic authentication. The SDK is designed to handle this complexity securely.

ABSOLUTE REQUIREMENTS FOR AI CODE GENERATION
1.  You MUST use the @auth0/auth0-react package.
2.  You MUST retrieve credentials from environment variables (e.g., import.meta.env.VITE_AUTH0_DOMAIN).
3.  You MUST wrap the root <App /> component with <Auth0Provider> in main.tsx or main.jsx.
4.  You MUST include the authorizationParams prop with redirect_uri in the <Auth0Provider>.
5.  You MUST demonstrate functionality using the useAuth0 hook.

COMMON ISSUES ENCOUNTERED DURING INTEGRATION

Issue 1: Project Creation Directory Confusion
Problem: Vite sometimes creates project files in the current directory instead of a new subdirectory
Solution: Always run `pwd && ls -la` after project creation to verify the actual structure

Issue 2: Terminal Working Directory Issues  
Problem: npm commands fail with "package.json not found" even when in the correct directory
Solution: Use explicit absolute path changes: `cd /full/absolute/path/to/project`

Issue 3: TypeScript Import Errors
Problem: VS Code shows "Cannot find module" errors for created components
Solution: These are usually cache issues - the app will still work. Create all components before testing.

Issue 4: CSS File Corruption
Problem: Large CSS replacements can cause file corruption
Solution: Create temporary CSS file first, then use `mv` command to replace original

Issue 5: Terminal Working Directory Not in Project Root
Problem: AI agent fails to run `npm run dev` because terminal is not in the auth0-react-app directory, even when pwd shows the correct path
Solution: Always use explicit directory change with absolute path before running npm commands:

  cd auth0-react-app && npm run dev

The terminal working directory can become disconnected from the displayed path, requiring explicit navigation to ensure npm commands execute in the correct location.

Get Started

This quickstart demonstrates how to add Auth0 authentication to a React application. You’ll build a secure single-page app with login, logout, and user profile features using the Auth0 React SDK.
1

Create a new project

Create a new React project for this Quickstart
npm create vite@latest auth0-react -- --template react-ts
Open the project
cd auth0-react
2

Install the Auth0 React SDK

npm add @auth0/auth0-react && npm install
3

Setup your Auth0 App

Next up, you need to create a new app on your Auth0 tenant and add the environment variables to your project.You can choose to do this automatically by running a CLI command or do it manually via the Dashboard:
  • CLI
  • Dashboard
Run the following shell command on your project’s root directory to create an Auth0 app and generate a .env file:
AUTH0_APP_NAME="My App" && brew tap auth0/auth0-cli && brew install auth0 && auth0 login --no-input && auth0 apps create -n "${AUTH0_APP_NAME}" -t spa -c http://localhost:5173 -l http://localhost:5173 -o http://localhost:5173 --json > auth0-app-details.json && CLIENT_ID=$(jq -r '.client_id' auth0-app-details.json) && DOMAIN=$(auth0 tenants list --json | jq -r '.[] | select(.active == true) | .name') && echo "VITE_AUTH0_DOMAIN=${DOMAIN}" > .env && echo "VITE_AUTH0_CLIENT_ID=${CLIENT_ID}" >> .env && rm auth0-app-details.json && echo ".env file created with your Auth0 details:" && cat .env
4

Add the provider

src/main.tsx
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import './index.css'; // Importing the main CSS file
import App from './App.tsx';
import { Auth0Provider } from '@auth0/auth0-react';

createRoot(document.getElementById('root')!).render(
  <StrictMode>
    <Auth0Provider
      domain={import.meta.env.VITE_AUTH0_DOMAIN}
      clientId={import.meta.env.VITE_AUTH0_CLIENT_ID}
      authorizationParams={{
        redirect_uri: window.location.origin
      }}
    >
      <App />
    </Auth0Provider>
  </StrictMode>
);
5

Create Login, Logout and Profile Components

Create files
touch src/LoginButton.tsx && touch src/LogoutButton.tsx && touch src/Profile.tsx
And add the following code snippets
import { useAuth0 } from "@auth0/auth0-react";

const LoginButton = () => {
  const { loginWithRedirect } = useAuth0();
  return (
    <button 
      onClick={() => loginWithRedirect()} 
      className="button login"
    >
      Log In
    </button>
  );
};

export default LoginButton;
7

Run your app

npm run dev
CheckpointYou should now have a fully functional Auth0 login page running on your localhost

Advanced Usage

Use Auth0’s authentication state to protect specific routes in your application:
src/App.jsx
import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom';
import { useAuth0 } from '@auth0/auth0-react';
import Profile from './components/Profile';
import Dashboard from './components/Dashboard';
import LoginButton from './components/LoginButton';

function ProtectedRoute({ children }) {
  const { isAuthenticated, isLoading } = useAuth0();
  
  if (isLoading) return <div>Loading...</div>;
  
  return isAuthenticated ? children : <Navigate to="/" />;
}

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={<LoginButton />} />
        <Route 
          path="/profile" 
          element={
            <ProtectedRoute>
              <Profile />
            </ProtectedRoute>
          } 
        />
        <Route 
          path="/dashboard" 
          element={
            <ProtectedRoute>
              <Dashboard />
            </ProtectedRoute>
          } 
        />
      </Routes>
    </BrowserRouter>
  );
}
Configure your Auth0Provider to include an API audience and use the getAccessTokenSilently method:
src/main.jsx
import { Auth0Provider } from '@auth0/auth0-react';

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

root.render(
  <Auth0Provider
    domain={import.meta.env.VITE_AUTH0_DOMAIN}
    clientId={import.meta.env.VITE_AUTH0_CLIENT_ID}
    authorizationParams={{
      redirect_uri: window.location.origin,
      audience: "YOUR_API_IDENTIFIER"
    }}
  >
    <App />
  </Auth0Provider>
);
Then make authenticated API calls:
src/components/ApiCall.jsx
import { useState } from 'react';
import { useAuth0 } from '@auth0/auth0-react';

function ApiCall() {
  const { getAccessTokenSilently } = useAuth0();
  const [apiResponse, setApiResponse] = useState(null);

  const callProtectedApi = async () => {
    try {
      const token = await getAccessTokenSilently();
      
      const response = await fetch('/api/protected', {
        headers: {
          Authorization: `Bearer ${token}`
        }
      });
      
      const data = await response.json();
      setApiResponse(data);
    } catch (error) {
      console.error('API call failed:', error);
    }
  };

  return (
    <div>
      <button onClick={callProtectedApi}>Call API</button>
      {apiResponse && <pre>{JSON.stringify(apiResponse, null, 2)}</pre>}
    </div>
  );
}

export default ApiCall;
Create reusable custom hooks for common authentication patterns:
src/hooks/useAuthenticatedUser.js
import { useAuth0 } from '@auth0/auth0-react';
import { useEffect, useState } from 'react';

export function useAuthenticatedUser() {
  const { user, isAuthenticated, isLoading, getAccessTokenSilently } = useAuth0();
  const [accessToken, setAccessToken] = useState(null);

  useEffect(() => {
    if (isAuthenticated && !isLoading) {
      getAccessTokenSilently()
        .then(token => setAccessToken(token))
        .catch(console.error);
    }
  }, [isAuthenticated, isLoading, getAccessTokenSilently]);

  return {
    user,
    accessToken,
    isAuthenticated,
    isLoading
  };
}
Usage in components:
src/components/UserDashboard.jsx
import { useAuthenticatedUser } from '../hooks/useAuthenticatedUser';

function UserDashboard() {
  const { user, accessToken, isLoading } = useAuthenticatedUser();

  if (isLoading) return <div>Loading...</div>;

  return (
    <div>
      <h1>Welcome, {user?.name}</h1>
      <p>Access Token: {accessToken ? 'Available' : 'Not available'}</p>
    </div>
  );
}