feat: add useAuth0Suspense hook for handling auth loading state with React 19+ - #1184
feat: add useAuth0Suspense hook for handling auth loading state with React 19+#1184gyaneshgouraw-okta wants to merge 3 commits into
Conversation
📝 WalkthroughWalkthrough
ChangesAuth0 Suspense integration
Estimated code review effort: 3 (Moderate) | ~20 minutes Suggested reviewers: Sequence Diagram(s)sequenceDiagram
participant Application
participant Auth0Provider
participant Auth0Client
participant useAuth0Suspense
participant Suspense
participant ErrorBoundary
Application->>Auth0Provider: render application
Auth0Provider->>Auth0Client: initialize Auth0
Application->>useAuth0Suspense: read authentication context
useAuth0Suspense->>Auth0Provider: read _initPromise
useAuth0Suspense-->>Suspense: suspend while initialization is pending
Auth0Client-->>Auth0Provider: resolve or reject initialization
Auth0Provider-->>useAuth0Suspense: provide initialized context or error
Suspense-->>Application: render initialized content
ErrorBoundary-->>Application: render initialization error
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/use-auth0-suspense.tsx`:
- Around line 10-13: Update Auth0SuspenseContextInterface and the
useAuth0Suspense return destructuring to omit _initPromise alongside isLoading
and error, ensuring the internal field is excluded from both the public type and
runtime result.
- Line 1: Update the import in use-auth0-suspense.tsx to use a React namespace
import, and change the hook’s use invocation to React.use while preserving
useContext and the existing behavior. This avoids requiring a static named use
export from older supported React versions.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: 1c9f4d54-bc0f-4e6e-8254-1c70f6a0094b
📒 Files selected for processing (7)
EXAMPLES.md__tests__/auth-provider.test.tsx__tests__/use-auth0-suspense.test.tsxsrc/auth0-context.tsxsrc/auth0-provider.tsxsrc/index.tsxsrc/use-auth0-suspense.tsx
Summary
Adds
useAuth0Suspense, a React 19-only hook that lets components read auth state declaratively: a<Suspense>boundary handles the loading state and a React Error Boundary handles initialization errors, removing the manualisLoadingchecks thatuseAuth0requires.The change is additive and non-breaking, the core path (
Auth0Provider,useAuth0) stays compatible with React 16.11–19,peerDependenciesis unchanged, and the new hook is gated at runtime so React 16–18 consumers get a clear error instead of a crash if they call it.Changes
Auth0Providernow creates a stable initialization promise that resolves when auth init succeeds and rejects (with the same error surfaced onerror) when it fails, exposed as an internal, unsupported context field.useAuth0Suspense()hook reads that promise via React 19'suse(): it suspends until auth is ready (so the nearest<Suspense>fallback renders) and re-throws init errors to the nearest Error Boundary.useAuth0interface minusisLoading(always resolved) anderror(thrown), including all auth methods (loginWithRedirect,logout,getAccessTokenSilently, …).React.useis unavailable, and a "must be used within an<Auth0Provider>" error when used outside a provider.Auth0SuspenseContextInterfacetype.Example
Testing
npm test- full suite passing at 100% coverage.Auth0Providertests extended to verify the init promise resolves on success, rejects on failure, and keeps a stable reference across re-renders.