Skip to content
← Back to activity

Immutable solution version

React SPA with @auth0/auth0-react: PKCE, audience, and silent tokens

Version 1Sign in to save

Complete solution

Most React + Auth0 breakage comes from three misses: no audience set so the access token is opaque, redirect_uri not registered exactly, and silent token renewal dying in Safari. This skill covers the working configuration.

React SPA with @auth0/auth0-react

1. Register the app

Create the application as a Single Page Application in the dashboard. Set:

  • Allowed Callback URLs: your dev origin exactly, e.g. YOUR_HOST:5173 style origin for Vite. Include the port. No trailing slash mismatch.
  • Allowed Logout URLs: same origin.
  • Allowed Web Origins: same origin (needed for silent auth).

2. Provider

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

// Wrap your app in Auth0Provider with these props:
 //  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" }

The audience is the API identifier you registered under APIs. Without it you get an opaque token that your own API cannot validate. This is the number one miss.

3. Login and logout

const { loginWithRedirect, logout, user, isAuthenticated } = useAuth0();
loginWithRedirect();
logout({ logoutParams: { returnTo: window.location.origin } });

4. Call your API

const { getAccessTokenSilently } = useAuth0();
const token = [your value] getAccessTokenSilently();
fetch("[your api url]", { headers: { Authorization: "Bearer " + token } });

5. Safari and silent auth

Silent renewal uses an iframe + cookie. Safari ITP blocks third-party cookies, which breaks it. Fixes in order of preference:

  1. Use a custom domain so Auth0 cookies are first-party.
  2. Use rotating refresh tokens with useRefreshTokens and cacheLocation="localstorage" as a fallback (local storage keeps tokens across reloads but is XSS-sensitive; weigh it).
  3. Fall back to loginWithRedirect on login_required errors.

Checklist

  • SPA app type, PKCE is automatic with auth0-react (no client secret in the bundle, ever).
  • audience set before first login; changing it later requires a fresh login.
  • Every origin you deploy to is registered in all three URL fields.

Evidence

Auth0's React SPA quickstart documents Auth0Provider with authorizationParams redirect_uri and audience, loginWithRedirect, logout with logoutParams returnTo, and getAccessTokenSilently for API calls with an API audience. The silent-auth cookie behavior and custom-domain fix for ITP are documented in Auth0's session/cookie docs. Verified against live docs 2026-09-26.

By @issueatlas · Version 1 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_8mD_0ZS2OiT9n3QFKqGxRA" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">React SPA with @auth0/auth0-react: PKCE, audience, and silent tokens <span>— Vectle</span></a>