-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Update the authentication guide to use useQuery within the AuthProvider context. #5700
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
WalkthroughA documentation example in the React authentication guide has been updated to demonstrate using TanStack Query's Changes
Sequence Diagram(s)sequenceDiagram
participant AuthProvider
participant useQuery as TanStack Query
participant useServerFn as useServerFn
participant Server
rect rgb(200, 220, 255)
Note over AuthProvider,Server: New Pattern with useQuery
AuthProvider->>useQuery: useQuery({ queryKey, queryFn })
useQuery->>useServerFn: call wrapped queryFn
useServerFn->>Server: getCurrentUserFn()
Server-->>useServerFn: user data
useServerFn-->>useQuery: return data
useQuery-->>AuthProvider: { data, isLoading, refetch }<br/>(+ cache, retry, stale time)
end
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches🧪 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (1)
docs/start/framework/react/guide/authentication.md (1)
126-164: Consider adding a comment explaining the queryFn pattern.The pattern
queryFn: useServerFn(getCurrentUserFn)is noteworthy—it wraps the server function call. Adding a brief inline comment would help readers understand whyuseQueryis used here instead of callinguseServerFndirectly (i.e., for query caching, automatic refetching, and lifecycle management).Consider this addition:
export function AuthProvider({ children }: { children: ReactNode }) { + // useQuery wraps the server function to provide caching, refetch, and loading state management const { data: user, isLoading, refetch } = useQuery({ queryKey: ["auth"], queryFn: useServerFn(getCurrentUserFn), })
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
docs/start/framework/react/guide/authentication.md(1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
docs/**/*.{md,mdx}
📄 CodeRabbit inference engine (AGENTS.md)
Use internal docs links relative to the docs/ folder (e.g., ./guide/data-loading)
Files:
docs/start/framework/react/guide/authentication.md
docs/{router,start}/**
📄 CodeRabbit inference engine (AGENTS.md)
Place router docs under docs/router/ and start framework docs under docs/start/
Files:
docs/start/framework/react/guide/authentication.md
🔇 Additional comments (1)
docs/start/framework/react/guide/authentication.md (1)
145-155: No issues found – theuseQueryAPI usage is correct.The project uses @tanstack/react-query v5.90.0, which returns properties including
data,isLoading(derived from isPending && isFetching), andrefetch. The code's destructuring of{ data: user, isLoading, refetch }matches the actual v5.x return type and requires no changes.Likely an incorrect or invalid review comment.
| const { data: user, isLoading, refetch } = useQuery({ | ||
| queryKey: ["auth"], | ||
| queryFn: useServerFn(getCurrentUserFn), | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing useQuery import statement.
The example uses useQuery at line 146 but the code block doesn't import it. This will cause a runtime error for developers copying the example. Add the import at the top of the code block.
Apply this diff to add the missing import:
// contexts/auth.tsx
import { createContext, useContext, ReactNode } from 'react'
import { useServerFn } from '@tanstack/react-start'
+import { useQuery } from '@tanstack/react-query'
import { getCurrentUserFn } from '../server/auth'Committable suggestion skipped: line range outside the PR's diff.
🤖 Prompt for AI Agents
In docs/start/framework/react/guide/authentication.md around lines 146 to 149,
the example uses useQuery but does not import it; add an import for useQuery
from the React Query package (e.g., @tanstack/react-query) at the top of the
code block so the example runs without a runtime error.
The guide on authentication implies that
useServerFnhas a similar api touseQuery:But this is not the correct return shape if the guide is followed; instead useServerFn will be returning a promise here. As such I have updated this component by wrapping the
userServerFnwithuseQuery, to avoid any confusion.I am not sure if this is deliberate sudo-code here - but I think this suggestion is correct for the example provided, and also aligns with the useage guide on using server functions in components: https://tanstack.com/start/latest/docs/framework/react/guide/server-functions#where-to-call-server-functions
Summary by CodeRabbit