AbhishekA
Convex Community2y ago
7 replies
Abhishek

Need suggestion on how to store user

I followed the SASS starter approach of storing user in database through useEffect inside a provider component.

But I am having this race condition where the provider renders a child which is a server component that require user to be in DB and its throwing error as we are storing user through client component provider.

To solve this to make the child a client component but I wanted to ask is there any other way to go around this ? Like calling store user through server component but then how to condition it so that its not called everytime

Provider Component
<ConvexProviderWithClerk client={convex} useAuth={useAuth}>
      <Authenticated>
        <StoreUserInDatabase />
        {children}
      </Authenticated>
      <AuthLoading>
        <Loading />
      </AuthLoading>
    </ConvexProviderWithClerk>

function StoreUserInDatabase() {
  const { user } = useUser();
  const storeUser = useMutation(api.users.store);
  useEffect(() => {
    void storeUser();
  }, [storeUser, user?.id]);
  return null;
}
Was this page helpful?