LukeL
Convex Community9mo ago
3 replies
Luke

Race condition when calling mutation after sign in

When using Convex Auth, I'm experiencing what appears to be a race condition when calling a mutation after signing in for the first time.

At the end of my onboarding flow I sign the user in anonymously and then save their onboarding responses to a "userProfile" table

await signIn("anonymous");

await saveOnboardingData({
  name: userName,
  selectedColor: selectedColor || "grey",
});


Inside the mutation I am doing the following

export const saveOnboardingData = mutation({
  args: {
    name: v.string(),
    selectedColor: v.string(),
  },
  handler: async (ctx, args) => {
    const userId = await getAuthUserId(ctx);
    if (!userId) throw new Error("Not authenticated");

    const userData = {
      userId,
      name: args.name,
      selectedColor: args.selectedColor,
    };

    await ctx.db.insert("userProfiles", userData);
  },
});


But calling this results in the "Not authenticated" error.

If I put in a 1 second delay between the sign in and the mutation call it works fine, but of course thats a hack that could fall apart.
Was this page helpful?