#Persisting username into convex database
8 messages · Page 1 of 1 (latest)
Hi @finite snow. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
To persist the username into the Convex database, you need to customize the user information in your sign-up form and write these to your users documents. Here are the steps you can follow:
-
Customize the schema to define the additional fields. You can refer to the guide on how to customize the schema (https://labs.convex.dev/auth/setup/schema).
-
Return the additional fields from the
profilemethod. Here's an example of how to do this:
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,
};
},
});
In this example, the profile method is returning an object that includes the name field. This is where you can include the username from your sign-up form.
- In your
createOrUpdateUsercallback, make sure to include thenamefield when creating or updating a user. Here's an example:
export const { auth, signIn, signOut, store } = convexAuth({
providers: [
Password,
],
callbacks: {
async createOrUpdateUser(ctx, args) {
if (args.existingUserId) {
await ctx.db.patch(args.existingUserId, {
email: args.profile.email,
name: args.profile.name, // Include the name here
});
return args.existingUserId;
} else {
return await ctx.db.insert('users', {
role: 'user',
email: args.profile.email,
name: args.profile.name, // And here
});
}
},
},
session: {
totalDurationMs: 1000 * 60 * 60 * 24 * 30, // 90 days
inactiveDurationMs: 1000 * 60 * 60 * 24 * 7, // 7 days
},
});
In this example, the name field is included when creating or updating a user in the database.
Please refer to the following sources for more information:
- Customize user information (https://labs.convex.dev/auth/config/passwords#customize-user-information)
- Storing Users in the Convex Database (https://docs.convex.dev/auth/database-auth)
- Discord support thread: [ConvexAuth]: additional fields passed to signIn, added to user document? (https://discord.com/channels/1019350475847499849/1270382038951792712)
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@finite snow kapa.ai is still learning and improving, please let us know how it did by reacting below
@stiff delta where do I define this in my code
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,
};
},
});
Let me check the knowledge base for you. One moment! :books:
The code you provided is a configuration for the Password provider in Convex. This should be defined in the convex/auth.ts file in your project. This file is where you set up your authentication providers and their configurations.
Here's an example of how your convex/auth.ts file might look:
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,
};
},
});