#Running a mutation after sign-in

11 messages · Page 1 of 1 (latest)

blissful phoenix
#

How do I run a mutation after the user signs in or creates an account? I'm making an app where users can sign up for a course. I need to enroll them in the course as soon as they sign up or log in from that course's page.

I'm using NextJS.

#

I tried, in my React login form event handler putting:

        const result = await signIn("password", formData);

        if (result.signingIn) {
          await createProfileAndEnrollment({
            cohortId: cohortId as Id<"cohorts">,
          });

The mutation fails when it tries to get the auth ID:

export const createProfileAndEnrollment = mutation({
  args: {
    cohortId: v.id("cohorts"),
    name: v.optional(v.string()),
  },
  handler: async (ctx, args) => {
    const userId = await getAuthUserId(ctx);
    if (!userId) {
      throw new Error("Authentication required");
    }
vital lion
blissful phoenix
#

Thanks. I need to enroll them in a specific cohort though. How do I pass in the cohortId to this callback?

blissful phoenix
#

Wow, this is overly complicated. I hope there's improvements coming for this flow.

vital lion
#

What improvements would you suggest?

blissful phoenix
#

If the way I tried it originally just worked. It seems it's not working because of a race condition. I ended up just changing the UX so the user has to click a button after signing in to run the mutation. That extra delay it takes for them to see and click the button is enough work around it.

Of course, this UX sucks but I'm in MVP build mode so don't have time to write a new provider.

#

Or if I could specify an argument to signIn to specify the mutation to run after, along with the mutation arguments

blissful phoenix
#

Or if I could call signIn from within a mutation

vital lion
#

Yeah, fair