I want to get the is Authenticated in the same initial load useEffect. Using isAuthenticated mean that i should but it into effect dependencies resulting in wrong effect behavior since i want the effect to run only on load/mount. Having a getAuthState() would be super useful in this scenario since i can await it inside the effect itself and mainainijg an empty dependency array
#Does convex provide getAuthState
4 messages · Page 1 of 1 (latest)
Hi @lunar jetty. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
Based on the provided knowledge sources, Convex does not provide a built-in getAuthState() function for the standard React client that you can await inside a useEffect.
The primary way to access authentication state in Convex for React is through the useConvexAuth() hook, which provides the isAuthenticated and isLoading booleans [Understanding useConvexAuth(); useConvexAuth].
Current Limitations and Workarounds
The knowledge sources highlight that managing this state within React effects can be complex because authentication state transitions are asynchronous and handled by the Convex client's internal state machine [Discord support thread: Roast of the convex client and authentication].
If you need to ensure an effect only acts when authentication is definitively known, you typically have two options:
- Using
useConvexAuthwith dependencies:
While you mentioned wanting an empty dependency array, the standard pattern is to includeisAuthenticatedandisLoadingto trigger the logic once the state transitions from loading to a final state.const { isAuthenticated, isLoading } = useConvexAuth(); useEffect(() => { if (!isLoading && isAuthenticated) { // Run your mount logic here once authenticated } }, [isLoading, isAuthenticated]);
- Skipping Queries/Logic:
To prevent logic from running before authentication is confirmed, you can use the"skip"pattern in Convex hooks, or wrap your component in the<Authenticated>helper component. Components inside<Authenticated>are guaranteed to have an authenticated user once they render [Skipping Queries When the User is Not Authenticated; React].
Note on SvelteKit and SSR
Interestingly, there is a getAuthState function mentioned specifically for SvelteKit integrations (using the convex-better-auth-svelte library) to determine authentication status from cookies during SSR [Get auth state in your layout server]. However, this is not a standard part of the core convex/react API for client-side effects.