feat: add useAuth0Suspense hook for handling auth loading state with React 19+ - #1184
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (6)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthrough
ChangesAuth0 Suspense integration
Estimated code review effort: 4 (Complex) | ~45 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
yogeshchoudhary147
left a comment
There was a problem hiding this comment.
Nice addition! Few things to fix before we merge:
- Move
useContextabove theReact.usecheck, hooks ordering issue (cschetan77's comment) - Wrap the rest-spread return in
useMemootherwise a new object gets created on every render (subhankarmaiti's comment) - The
_initPromiserejection is permanent so Error Boundary retries will not work (subhankarmaiti's comment) - Provider guard is fragile, please check cschetan77's suggestion on that
Fix these and we are good to go!
Hey @yogeshchoudhary147 |
**Added** - feat: add useAuth0Suspense hook for handling auth loading state with React 19+ [\#1184](#1184) ([gyaneshgouraw-okta](https://github.com/gyaneshgouraw-okta)) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Added API documentation for `useAuth0Suspense` and its suspense context type, including usage guidance and React 19 requirements. - Added the new API to the documentation index and search navigation. - **Documentation** - Refreshed generated API references, hierarchy links, source links, and search data for accuracy. - Updated documentation metadata across authentication, MFA, passkey, popup, and configuration APIs. - **Chores** - Bumped the package version to **2.24.0**. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
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.Summary by CodeRabbit
New Features
useAuth0Suspensefor React 19+ applications, enabling Auth0 initialization with React Suspense and Error Boundaries.Documentation
useAuth0Suspense, including setup requirements and error handling.