-
Notifications
You must be signed in to change notification settings - Fork 458
Description
When using WorkOS AuthKit with Convex in a Next.js App Router project, if a query requiring authentication is preloaded on the server (preloadQuery(..., { token })) and then consumed via usePreloadedQuery on the client, the client-side re-subscription sometimes fails because the Convex client has not yet authenticated over its websocket.
Steps to Reproduce
- Use
preloadQuery(api.secureQuery, {}, { token })in a Server Component. Make sureapi.secureQuerythrows on missing token. - Pass it into a Client Component that calls
usePreloadedQuery. - Observe: the first render shows data (SSR), but the client re-subscription throws an auth error
Expected Behavior
The Convex React client should wait until it’s authenticated (via the provided useAuth hook) before resubscribing, or at least handle the missing token gracefully.
Workaround
Wrap the client component in an AuthGate that waits for useConvexAuth() to report isLoading === false before mounting the query component.
Environment
- Next.js (App Router)
@workos-inc/authkit-nextjsv2.xconvexv1.x- Using
ConvexProviderWithAuth+useAuthfrom AuthKit.
Suggested Fix
I'm not sure whether this is an actual bug, or simply intended to be the developers concern. I have the following ideas:
- In a normal
useQueryI would be able to use "skip", so maybe it's possible to introduce that tousePreloadedQueryaswell. - An options object with a
waitForAuthfield. - A new primitive function
authenticatedQueryor rather a field insidequerythat indicates, that this particular query needs auth. Then the query would block on server or client while auth is loading (I'm not sure which is more appropriate). Then by defaultctx.authis not available, only if the flag is used. - Similar to the previous, but instead of explicitly defining it, deriving it from the usage of
ctx.authin the function.