"use client"; import useStoreUserEffect from "@/lib/useStoreUserEffect"; import { useKindeBrowserClient } from "@kinde-oss/kinde-auth-nextjs"; import { ConvexProviderWithAuth, ConvexReactClient } from "convex/react"; import { useCallback, useMemo } from "react"; type UseKindeAuth = () => { isLoading: boolean | null; isAuthenticated: boolean | null; getToken: () => string | null; }; function useUseAuthFromKinde(useAuth: UseKindeAuth) { return useMemo( () => function useAuthFromKinde() { const { isLoading, isAuthenticated, getToken } = useAuth(); const fetchAccessToken = useCallback( async ({ forceRefreshToken }: { forceRefreshToken: boolean }) => { try { return await getToken(); } catch (error) { return null; } }, [getToken] ); return useMemo( () => ({ isLoading: isLoading ?? false, isAuthenticated: isAuthenticated ?? false, fetchAccessToken, }), [isLoading, isAuthenticated, fetchAccessToken] ); }, [useAuth] ); } const convex = new ConvexReactClient(process.env.NEXT_PUBLIC_CONVEX_URL!); const useCurrentUser = () => { const { isLoading, isAuthenticated, getToken } = useKindeBrowserClient(); console.log("isAuthenticated", isAuthenticated); return { isLoading, isAuthenticated, getToken }; }; export function ConvexClientProvider({ children, }: { children: React.ReactNode; }) { { const useAuthFromKinde = useUseAuthFromKinde(useCurrentUser); // useStoreUserEffect(); return ( <ConvexProviderWithAuth client={convex} useAuth={useAuthFromKinde}> {children} </ConvexProviderWithAuth> ); } }
layout.tsx
export default function RootLayout({ children, }: Readonly<{ children: React.ReactNode; }>) { return ( <html lang="en" suppressHydrationWarning> <body className={inter.className}> <ConvexClientProvider>{children}</ConvexClientProvider> </body> </html> ); }
<LoginLink>
is not working. I tried to then login through the register but the state is just false
when logging the authentication bool state you've inserted.page.tsx
⬇️mport { LoginLink, RegisterLink } from "@kinde-oss/kinde-auth-nextjs"; export default function Home() { return ( <main className=""> <LoginLink>Login</LoginLink> <RegisterLink>Register</RegisterLink> </main> ); }
import { useKindeBrowserClient } from "@kinde-oss/kinde-auth-nextjs"; import { useCallback, useMemo } from "react"; interface UseAuthReturn { isLoading: boolean; isAuthenticated: boolean; fetchAccessToken: (args: { forceRefreshToken: boolean; }) => Promise<string | null>; } export function useAuthFromKinde(): UseAuthReturn { const { isLoading, isAuthenticated, getAccessTokenRaw, accessToken } = useKindeBrowserClient(); const fetchAccessToken = useCallback( async ({ forceRefreshToken }: { forceRefreshToken: boolean }) => { if (forceRefreshToken) { try { const response = await getAccessTokenRaw(); // Returns the token as string return response; } catch (error) { return null; } } // Add this line to ensure the function always returns a string or null return null; }, [accessToken], ); return useMemo( () => ({ isLoading: isLoading ?? false, isAuthenticated: isAuthenticated ?? false, fetchAccessToken, }), [isLoading, isAuthenticated, fetchAccessToken], ); }