Luke
Luke3mo ago

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.
3 Replies
Convex Bot
Convex Bot3mo ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
jdboachie
jdboachie3mo ago
Experienced the same thing. What you could do is use the afterUserCreatedOrUpdated callback (not sure if I got the function name right) In the docs: https://labs.convex.dev/auth/advanced#writing-additional-data-during-authentication If you do use this method, you might want to find a way to differentiate between a user creation and user update event Hope this helps!
Advanced: Details - Convex Auth
Authentication library for your Convex backend
Luke
LukeOP3mo ago
Thanks for the tip! However as this is a callback on the backend its not obvious how I'd leverage that to then create the profile with the info they enter in Onboarding. I may have to rethink my flow for this

Did you find this page helpful?