How do I make username a required field during sign up?

// auth.ts
export const auth = betterAuth({
  appName: 'appname',
  database: drizzleAdapter(db, {
    provider: 'pg',
    usePlural: true,
    schema: {
      users: UserTable,
      sessions: SessionTable,
      accounts: AccountTable,
      verifications: VerificationTable,
    },
  }),
  user: {
    additionalFields: {
      username: {
        type: 'string',
        required: true,
      },
    },
  },
  plugins: [
    emailOTP({
      async sendVerificationOTP({ email, otp, type }) {
        // send email
      },
      sendVerificationOnSignUp: true,
      otpLength: 6,
      expiresIn: 10 * 60,
      generateOTP(data, request) {
        return '123456';
      },
    }),
    username({
      minUsernameLength: 3,
      maxUsernameLength: 30,
      usernameValidator: (username) => {
        // Only allow alphanumeric characters, underscores, and dots
        return /^[a-zA-Z0-9_.]+$/.test(username);
      },
    }),
    openAPI(),
    expo(),
  ],
  emailAndPassword: {
    enabled: true,
  }
});


// schema.ts
export const UserTable = pgTable('users', {
  id: text('id').primaryKey(),
  name: text('first_name').notNull(),
  email: text('email').notNull().unique(),
  emailVerified: boolean('email_verified').notNull(),
  phone: varchar(),
  image: text('image'),
  role: userRole('role').default('user').notNull(),
  banned: boolean('banned'),
  banReason: text('ban_reason'),
  banExpires: timestamp('ban_expires', { withTimezone: true }),
  username: text('username').notNull().unique(),
  displayUsername: text('display_username'),
  ...timestamps,
});


When I call the sign up with email endpoint with this payload,
{
  "name": "Farhan Farooqui",
  "email": "neetrasaudi3@gmail.com",
  "password": "qwerty12345"
}

I get error from DB, "insert into users failed..." instead of better auth's error
Was this page helpful?