11'use client' ;
22
3- import { ReactNode , useCallback , useRef } from 'react' ;
3+ import { ReactNode , useCallback , useRef , useState } from 'react' ;
44import { ConvexReactClient } from 'convex/react' ;
55import { ConvexProviderWithAuth } from 'convex/react' ;
66import { AuthKitProvider , useAuth , useAccessToken } from '@workos-inc/authkit-nextjs/components' ;
77
8- const convex = new ConvexReactClient ( process . env . NEXT_PUBLIC_CONVEX_URL ! ) ;
9-
108export function ConvexClientProvider ( { children } : { children : ReactNode } ) {
9+ const [ convex ] = useState ( ( ) => {
10+ return new ConvexReactClient ( process . env . NEXT_PUBLIC_CONVEX_URL ! ) ;
11+ } ) ;
1112 return (
1213 < AuthKitProvider >
1314 < ConvexProviderWithAuth client = { convex } useAuth = { useAuthFromAuthKit } >
@@ -20,8 +21,9 @@ export function ConvexClientProvider({ children }: { children: ReactNode }) {
2021function useAuthFromAuthKit ( ) {
2122 const { user, loading : isLoading } = useAuth ( ) ;
2223 const { accessToken, loading : tokenLoading , error : tokenError } = useAccessToken ( ) ;
23- const loading = ( isLoading ?? false ) || ( tokenLoading ?? false ) || ! accessToken ;
24- const authenticated = ! ! user && ! ! accessToken && ! loading ;
24+ const hasIncompleteAuth = ( ! ! user && ! accessToken ) || ( ! user && ! ! accessToken ) ;
25+ const loading = ( isLoading ?? false ) || ( tokenLoading ?? false ) || hasIncompleteAuth ;
26+ const authenticated = ! ! user && ! ! accessToken ;
2527
2628 // Memoize the token to prevent unnecessary changes
2729 const stableAccessToken = useRef < string | null > ( null ) ;
0 commit comments