#Unauthorized: No identity found when calling router.refresh() / window.location.href = "/"
1 messages · Page 1 of 1 (latest)
Hi @true slate. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
When you perform a programmatic hard refresh (e.g., using router.refresh() or window.location.href = "/") in a Convex + Clerk app, it's common to see a brief "Unauthorized: No identity found" error. This happens because, on page load, the Convex client needs a moment to re-authenticate with the backend. During this window, any queries or mutations that require authentication may run before the user's identity is available, resulting in errors like "No identity found" or "User not authenticated" in your logs.
This is a known race condition, especially in apps using SSR or route loaders that prefetch queries before the authentication state is fully established. The Convex team and community recommend a few approaches to mitigate this:
-
Skip Queries Until Authenticated:
Use theskipargument in your queries so they only run when the user is authenticated. For example, in React:const { user } = useUser(); const data = useQuery( api.something.getData, user ? { ...args } : "skip" );This prevents queries from running with a null identity and avoids unnecessary errors during the authentication window. This approach is confirmed as correct and intended by Convex support staff and community members Discord thread.
-
Wrap Components with
<Authenticated>:
If you're using Clerk, wrap components that make authenticated queries with Clerk's<Authenticated>component. This ensures those components only render (and thus only run queries) when the user is authenticated Convex Clerk Auth Guide.
- Handle SSR and Route Loaders Carefully:
For SSR or TanStack route loaders, ensure you only prefetch queries after authentication is established, or pass the correct token explicitly to Convex queries/mutations. Otherwise, loaders may run before the Convex client is authenticated, causing the same issue Debugging Authentication TanStack/Clerk integration.
Summary:
This error is expected if queries run before authentication is ready. The recommended solution is to conditionally run queries only when the user is authenticated, using the skip pattern or by wrapping components in <Authenticated>. This will prevent the "No identity found" error on hard refreshes or navigation events.
If you continue to see this error even after implementing these patterns, double-check your Clerk and Convex integration, and ensure tokens are being passed correctly as described in the Convex docs and support threads.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@true slate Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@wise marsh is there a way to cancel all queries with the useConvex query client
Let me check the knowledge base for you again. One moment! :books:
I'm sorry, but the knowledge sources do not contain information about a method to cancel all queries using the Convex query client (such as with useConvex or ConvexReactClient). The available documentation and support threads discuss query execution, authentication, and skipping queries, but do not mention a built-in API for canceling all active queries at once.
If you need to prevent queries from running, the recommended approach is to use the "skip" argument or conditionally render components so that queries are not initiated when not needed, especially during authentication transitions. However, explicit cancellation of all queries is not documented in the provided sources.