how to map custom schema with better-auth

my drizzle tables

eg:
 export const Users = pgTable(
  "users",
  {
    id: serial("id").primaryKey(),
    organizationId: integer("organization_id")
      .references(() => Organization.id)
      .notNull(),
    firstName: varchar("first_name").notNull(),
    middleName: varchar("middle_name"),
    lastName: varchar("last_name", { length: 55 }).notNull(),
    email: text("email").notNull(),
    phone: varchar({ length: 15 }),
    emailVerified: boolean("email_verified").default(false),
    image: text("image"), })

how to map this tables to better-auth user table

import * as schema from "../../models";
import { Users } from "../../models/User";


const getAdditionalFields = (schema: PgTable) => {
  // biome-ignore lint/suspicious/noExplicitAny: <explanation>
  const fields: Record<string, any> = {};
  const defaultFields = [
    "id",
    "email",
    "image",
    "emailVerified",
    "createdAt",
    "updatedAt",
  ];

  for (const [key, value] of Object.entries(schema)) {
    if (!defaultFields.includes(key)) {
      fields[key] = {
        type: value.config?.type === "integer" ? "number" : "string",
        required: value.notNull ?? false,
      };
    }
  }

  return fields;
};

const additionalField = getAdditionalFields(Users);

export const auth = betterAuth({
  user: {
    additionalFields: additionalField,
  },


 

  secret: BETTER_AUTH_SECRET,
  baseURL: BETTER_AUTH_URL,
  advanced: {
    generateId: false,
  },
  database: drizzleAdapter(db, {
    provider: "pg",
    schema: {
      ...schema,
      user: schema.Users,
      account: schema.Account,
      session: schema.Session,
      verification: schema.Verification,
      organization: schema.Organization,
    },
  }),




});


this is my config

the error am getting
etterAuthError: The field "name" does not exist in the "user" schema. Please update your drizzle schema or re-generate using "npx @better-auth/cli generate".
Was this page helpful?