diff --git a/npm-packages/convex/src/react/ConvexAuthState.tsx b/npm-packages/convex/src/react/ConvexAuthState.tsx index 937edb89..0fa4e2c6 100644 --- a/npm-packages/convex/src/react/ConvexAuthState.tsx +++ b/npm-packages/convex/src/react/ConvexAuthState.tsx @@ -105,10 +105,17 @@ export function ConvexProviderWithAuth({ setIsConvexAuthenticated((isConvexAuthenticated) => isConvexAuthenticated ? false : null, ); - // Clear timeout later, so that queries from - // unmounted child components unsubscribe first - // and rerun without auth on the server - setTimeout(() => client.clearAuth(), 0); + }; + } + }, [isAuthenticated, fetchAccessToken, isLoading, client]); + + // Clear auth later, so that queries from + // unmounted child components unsubscribe first + // and rerun without auth on the server + useLayoutEffect(() => { + if (isAuthenticated) { + return () => { + client.clearAuth(); }; } }, [isAuthenticated, fetchAccessToken, isLoading, client]);