rgzR
Convex Community2y ago
7 replies
rgz

Using orCreateOrUpdate user in callback

Two problems here:
1. How can I get the tokenIdentity when creating a user, I need to get authSessionsId but I keep getting null.
my approach was ${url}/${userId}/${tokenId}

2. I keep getting a 302 error but everything is still working correctly.

GET /api/auth/signin/* 302
GET /api/auth/callback/* 302

This is the callback url I added on google:
https://my-stuff-123.convex.site/api/auth/callback/google

  callbacks: {
    async createOrUpdateUser(ctx, args) {
      let userId: Id<"users">;
      const profile = args.profile as ExtendedProfile;
      const provider = args.provider;

      const existingUser = await ctx.db.query("users")
        .filter((q) => q.eq(q.field("email"), args.profile.email))
        .first();

      if (existingUser) {
        userId = existingUser._id as Id<"users">;

        await ctx.db.patch<Id<"users">>(existingUser._id, {
          providers: [...existingUser.providers, provider.id],
        });

      } else {
        const newUser = await ctx.db.insert("users", {
          email: profile?.email,
          emailVerified: true,
          emailVerificationTime: new Date().getTime(),
          name: profile?.name,
          firstName: profile?.given_name,
          lastName: profile?.family_name,
          image: profile?.image,
          isOnboardingComplete: false,
          orgIds: [],
          providers: [provider.id],
          tokenIdentifier: "",
        });

        userId = newUser as Id<"users">;
      }

      return userId;
    },
  },
Was this page helpful?