#how to insert additional data about a user on sign up
9 messages · Page 1 of 1 (latest)
Hi @steady epoch. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
To insert additional data about a user at sign up when using Convex Auth with a custom users schema (including non-optional fields), you need to:
- Customize your users table schema to include the additional required field(s).
- Return those fields from the provider's
profilemethod so that the data is available during user creation.
Example
Suppose your users table requires a non-optional role field:
// convex/schema.ts
import { defineSchema, defineTable } from "convex/server";
import { v } from "convex/values";
import { authTables } from "@convex-dev/auth/server";
const schema = defineSchema({
...authTables,
users: defineTable({
email: v.string(),
name: v.string(),
role: v.string(), // non-optional field
// other fields...
}),
});
export default schema;
Then, in your custom password provider, return the required field from the profile method:
// CustomProfile.ts
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, // must be present in params
};
},
});
On your sign-up form, ensure you include the additional field (e.g., role) in the FormData passed to signIn:
onSubmit={(e) => {
e.preventDefault();
const formData = new FormData(e.target as HTMLFormElement);
formData.set("role", "user"); // or get from user input
void signIn("password", formData).catch((error) => {
setError(error.message);
});
}}
Important:
- The field must be present in the form data and returned by the
profilefunction. - If the field is missing or not returned, you'll get a schema validation error on user creation.
This approach is documented in the Convex Auth docs for customizing user information during sign-up: Customize user information and is confirmed by community discussions and examples in Discord threads.
If you follow these steps and still encounter issues, double-check that your form is passing the required field and that your profile function is returning it. If the field is required in your schema, it must always be present during sign-up, or user creation will fail.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@steady epoch Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@quasi flame thats not what im looking for. Im talking about github oauth for now, and I don't want to set the data from the client, the server needs to set it.
Let me check the knowledge base for you again. One moment! :books:
To insert additional data about a user on sign up using GitHub OAuth (or any OAuth provider) in Convex Auth, and have the server set the value (not the client), you should use the provider's profile method or implement the createOrUpdateUser callback.
Option 1: Use the profile method
You can customize the data inserted into the users table by returning additional fields from the profile method in your OAuth provider config. For example, you can set a default value for your custom field here:
import GitHub from "@auth/core/providers/github";
import { convexAuth } from "@convex-dev/auth/server";
export const { auth, signIn, signOut, store, isAuthenticated } = convexAuth({
providers: [
GitHub({
profile(githubProfile, tokens) {
return {
id: githubProfile.id + "", // ensure string
name: githubProfile.name,
email: githubProfile.email,
image: githubProfile.picture,
myCustomField: "defaultValue", // set by server
};
},
}),
],
});
Make sure your users schema includes myCustomField as a required field. This way, every new user created via GitHub OAuth will have this field set by the server, not the client.
See: Retrieving other profile information
Option 2: Use createOrUpdateUser callback
If you need more control (for example, setting the field based on more complex logic), implement the createOrUpdateUser callback. This function runs on the server during user creation and can set any fields you want:
import GitHub from "@auth/core/providers/github";
import { convexAuth } from "@convex-dev/auth/server";
export const { auth, signIn, signOut, store, isAuthenticated } = convexAuth({
providers: [GitHub],
callbacks: {
async createOrUpdateUser(ctx, args) {
if (args.existingUserId) {
return args.existingUserId;
}
// Set your custom field here
return ctx.db.insert("users", {
email: args.profile.email,
name: args.profile.name,
myCustomField: "serverValue", // set by server
// ...other fields
});
},
},
});
[See: Controlling user creation and account linking behavior](<https://labs.convex.dev/auth/advanced#controlling-user-creation-and-account-linking-behavior>)
---
**Summary:**
- Use the `profile` method to set static or profile-derived values for new users.
- Use `createOrUpdateUser` for full control and to set any server-side values during user creation.
No client input is needed for these fields; the server sets them during the authentication flow.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------