#Convex Auth

5 messages · Page 1 of 1 (latest)

wheat flicker
#

I want to create a profile for the user when they signup, how can I do this using Convex Auth?

opaque zenithBOT
#

Hi @wheat flicker. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

To create a user profile at signup using Convex Auth, you have a couple of options depending on how much control you want over the user creation process and what additional data you want to store.

1. Customize the users Table Schema

First, define your custom fields in the users table schema. For example, if you want to store fields like name, role, or avatarUrl, add them to your schema:

import { defineSchema, defineTable } from "convex/server";
import { v } from "convex/values";
import { authTables } from "@convex-dev/auth/server";

const schema = defineSchema({
  ...authTables,
  users: defineTable({
    name: v.optional(v.string()),
    avatarUrl: v.optional(v.string()),
    email: v.optional(v.string()),
    // Add your custom fields here
    role: v.optional(v.string()),
  }).index("email", ["email"]),
  // Other tables...
});

export default schema;

Customizing Schema

2. Return Additional Fields from the Provider's profile Method

When using the Password provider (or other providers), you can return additional fields from the profile method. These fields will be included in the user document at signup:

import { Password } from "@convex-dev/auth/providers/Password";
import { DataModel } from "./_generated/dataModel";

export default Password<DataModel>({
  profile(params) {
    return {
      email: params.email as string,
      name: params.name as string,
      role: params.role as string, // Custom field
    };
  },
});

Customize user information

3. Use the createOrUpdateUser Callback for Full Control

#

If you need more control (e.g., conditional logic, additional writes), implement the createOrUpdateUser callback in your convexAuth config. This lets you fully customize how user documents are created or updated:

import { convexAuth } from "@convex-dev/auth/server";
import { Password } from "@convex-dev/auth/providers/Password";

export const { auth, signIn, signOut, store, isAuthenticated } = convexAuth({
  providers: [Password],
  callbacks: {
    async createOrUpdateUser(ctx, args) {
      // Custom logic to create or update user profile
      return await ctx.db.insert("users", {
        email: args.profile.email,
        name: args.profile.name,
        role: args.profile.role,
        // ...other fields
      });
    },
  },
});

Discord example

4. Use afterUserCreatedOrUpdated for Additional Writes

If you don't need to override the default user creation, you can use the afterUserCreatedOrUpdated callback to perform additional writes after a user is created:

export const { auth, signIn, signOut, store, isAuthenticated } = convexAuth({
  providers: [Password],
  callbacks: {
    async afterUserCreatedOrUpdated(ctx, { userId }) {
      await ctx.db.insert("profiles", { userId, ...otherProfileData });
    },
  },
});

Writing additional data during authentication


Summary:

  • Define your custom fields in the users schema.
  • Return those fields from the provider's profile method.
  • For more control, use the createOrUpdateUser callback.
  • For extra writes, use afterUserCreatedOrUpdated.
#

This approach ensures that a user profile is created and populated with your desired fields when a user signs up using Convex Auth.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: