#Persisting username into convex database

8 messages · Page 1 of 1 (latest)

finite snow
#

My signup form has email, username, and password fields. The email got written to the database on signup, but the name remains unset.
How can I fix this

stiff deltaBOT
#

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:

  1. 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).

  2. Return the additional fields from the profile method. 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.

  1. In your createOrUpdateUser callback, make sure to include the name field 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:


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

finite snow
#

@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,
};
},
});

stiff deltaBOT
#

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,
    };
  },
});

This convex/auth.ts file is part of the Convex setup in your project. If you don't have this file yet, you might need to set up Convex in your project first. You can follow the Convex setup guide (https://labs.convex.dev/auth/setup) for more information.

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