rgzR
Convex Community2y ago
4 replies
rgz

Customizing default Schema after OAuth login

I'm trying to use the default schema and just update it with a few items:

I updated my schema with a new users table and added two fields at the end:
  ...authTables,
  users: defineTable({
    email: v.optional(v.string()),
    emailVerificationTime: v.optional(v.float64()),
    image: v.optional(v.string()),
    isAnonymous: v.optional(v.boolean()),
    name: v.optional(v.string()),
    phone: v.optional(v.string()),
    phoneVerificationTime: v.optional(v.float64()),
    isOnboardingComplete: v.optional(v.boolean()),
    orgIds: v.optional(v.array(v.object({
      id: v.id("organization"),
      role: v.union(v.literal("org:admin"), v.literal("org:member"))
    }))),
  })
    .index("by_email", ["email"])
    .index("by_phone", ["phone"]),


In the auth I used the createOrUpdateUser callback in auth.ts to create the user:
export const { auth, signIn, signOut, store } = convexAuth({
  providers: [
    GitHub,
    Google,
    ResendOTP,
    Password,
    Password({ id: "password-with-reset", reset: ResendOTPPasswordReset }),
    Password({
      id: "password-code",
      reset: ResendOTPPasswordReset,
      verify: ResendOTP,
    }),
    Password({ id: "password-link", verify: Resend }),
  ],
  callbacks: {
    async createOrUpdateUser(ctx, args) {
      if (args.existingUserId) {
        return args.existingUserId;
      }

      return ctx.db.insert("users", {
        email: args.profile.email,
        emailVerified: args.profile.emailVerified,
        name: args.profile.name,
        phone: args.profile.phone,
        phoneVerified: args.profile.phoneVerified,
        isOnboardingComplete: false,
        orgIds: [],
      });
    },
  },
});
Was this page helpful?