[GH-ISSUE #1343] Additional Fields not setting default value (drizzle) #8701

Closed
opened 2026-04-13 03:51:54 -05:00 by GiteaMirror · 4 comments
Owner

Originally created by @MarkForLoop on GitHub (Feb 3, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/1343

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

EX:
I ran:
"auth:generate": "pnpm dlx @better-auth/cli@latest generate --y --output ./lib/server/schema/auth.schema.ts && prettier --write ./lib/server/schema/auth.schema.ts",
to generate the auth and then push it to the database (supabase)

when I ran it locally I and it says isLocked it required (look at image attached), but set input to be false so the user can't set it during their sign up which force it to go to the default.
this is the code that I have for the sign-up

    await authClient.signUp.email(
      {
        email: data.email,
        password: data.password,
        firstName: data.firstName,
        lastName: data.lastName,
        name: `${data.firstName} ${data.lastName}`,
        dob: new Date("1997-10-05"),
      },
      {
        onRequest: (ctx) => {
          console.log("🚀 ~ onSubmit onRequest ~ ctx:", ctx);
          //show loading
          setIsLoading(true);
        },
        onSuccess: (ctx) => {
          console.log("🚀 ~ onSubmit onSuccess ~ ctx:", ctx);
          setIsLoading(false);
          //redirect to the dashboard
        },
        onError: (ctx) => {
          console.log("🚀 ~ onSubmit onError ~ ctx:", ctx);
          setIsLoading(false);
          alert(ctx.error.message);
        },
      },
    );

Image

Current vs. Expected behavior

Current behaviour

export const user = pgTable("user", {
  id: text("id").primaryKey(),
  name: text("name").notNull(),
  email: text("email").notNull().unique(),
  emailVerified: boolean("email_verified").notNull(),
  image: text("image"),
  createdAt: timestamp("created_at").notNull(),
  updatedAt: timestamp("updated_at").notNull(),
  phoneNumber: text("phone_number").unique(),
  phoneNumberVerified: boolean("phone_number_verified"),
  firstName: text("first_name").notNull(),
  lastName: text("last_name").notNull(),
  dob: timestamp("dob").notNull(),
  rollingNetDepositLimit: integer("rolling_net_deposit_limit").notNull(),
  rollingNetDepositLimitPeriod: text("rolling_net_deposit_limit_period").notNull(),
  isLocked: boolean("is_locked").notNull(),
});

Expected behaviour

export const user = pgTable("user", {
  id: text("id").primaryKey(),
  name: text("name").notNull(),
  email: text("email").notNull().unique(),
  emailVerified: boolean("email_verified").notNull(),
  image: text("image"),
  createdAt: timestamp("created_at").notNull(),
  updatedAt: timestamp("updated_at").notNull(),
  phoneNumber: text("phone_number").unique(),
  phoneNumberVerified: boolean("phone_number_verified"),
  firstName: text("first_name").notNull(),
  lastName: text("last_name").notNull(),
  dob: timestamp("dob").notNull(),
  rollingNetDepositLimit: integer("rolling_net_deposit_limit").notNull().default(160000),
  rollingNetDepositLimitPeriod: text("rolling_net_deposit_limit_period").notNull().default("30_days"),
  isLocked: boolean("is_locked").notNull().default(false),
});

What version of Better Auth are you using?

1.1.15

Provide environment information

- OS: macOS (Sequoia 15.1.1)
- browser: Arc

Which area(s) are affected? (Select all that apply)

Backend

Auth config (if applicable)

import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { APIError, createAuthMiddleware } from "better-auth/api";
import { phoneNumber } from "better-auth/plugins";
import { db } from "./db";
import * as schema from "./schema";

export const auth = betterAuth({
  baseURL: process.env.VITE_BASE_URL,
  appName: "Edify",

  database: drizzleAdapter(db, {
    provider: "pg",
    schema: { ...schema },
  }),
  emailAndPassword: {
    enabled: true,
  },
  // custom model
  user: {
    additionalFields: {
      firstName: {
        type: "string",
        required: true,
        fieldName: "first_name",
      },
      lastName: {
        type: "string",
        required: true,
        fieldName: "last_name",
      },
      dob: {
        type: "date",
        required: true,
        fieldName: "dob",
      },
      rollingNetDepositLimit: {
        type: "number",
        required: true,
        fieldName: "rolling_net_deposit_limit",
        defaultValue: 160000,
        input: false,
      },
      rollingNetDepositLimitPeriod: {
        type: "string",
        required: true,
        fieldName: "rolling_net_deposit_limit_period",
        defaultValue: "30_days",
        input: false,
      },
      isLocked: {
        type: "boolean",
        required: true,
        fieldName: "is_locked",
        input: false,
        defaultValue: false,
      },
    },
  },

  // hooks
  databaseHooks: {
    user: {
      create: {
        before: async (user) => {
          console.log("🚀 ~ before: ~ user:", user);

          return {
            data: {
              ...user,
            },
          };
        },
        after: async (user) => {
          // create user on stripe here
          console.log("🚀 ~ after: ~ user:", user);
        },
      },
    },
  },
  hooks: {
    before: createAuthMiddleware(async (ctx) => {
      if (ctx.path !== "/sign-up/email") {
        return;
      }
    }),
  },

  plugins: [
    phoneNumber({
      sendOTP: async ({ code, phoneNumber }) => {
        console.log("🚀 ~ sendOTP ~ ctx:", code, phoneNumber);
      },
    }),
  ],
});

Additional context

I first created this on discord: https://discord.com/channels/1288403910284935179/1335917057442713620

I am also using Tanstack Start

Originally created by @MarkForLoop on GitHub (Feb 3, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/1343 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce EX: I ran: ` "auth:generate": "pnpm dlx @better-auth/cli@latest generate --y --output ./lib/server/schema/auth.schema.ts && prettier --write ./lib/server/schema/auth.schema.ts", ` to generate the auth and then push it to the database (supabase) when I ran it locally I and it says isLocked it required (look at image attached), but set input to be false so the user can't set it during their sign up which force it to go to the default. this is the code that I have for the sign-up ``` await authClient.signUp.email( { email: data.email, password: data.password, firstName: data.firstName, lastName: data.lastName, name: `${data.firstName} ${data.lastName}`, dob: new Date("1997-10-05"), }, { onRequest: (ctx) => { console.log("🚀 ~ onSubmit onRequest ~ ctx:", ctx); //show loading setIsLoading(true); }, onSuccess: (ctx) => { console.log("🚀 ~ onSubmit onSuccess ~ ctx:", ctx); setIsLoading(false); //redirect to the dashboard }, onError: (ctx) => { console.log("🚀 ~ onSubmit onError ~ ctx:", ctx); setIsLoading(false); alert(ctx.error.message); }, }, ); ``` ![Image](https://github.com/user-attachments/assets/67cfa377-14bd-4d6c-8e92-a4dcbfe4a415) ### Current vs. Expected behavior Current behaviour ``` export const user = pgTable("user", { id: text("id").primaryKey(), name: text("name").notNull(), email: text("email").notNull().unique(), emailVerified: boolean("email_verified").notNull(), image: text("image"), createdAt: timestamp("created_at").notNull(), updatedAt: timestamp("updated_at").notNull(), phoneNumber: text("phone_number").unique(), phoneNumberVerified: boolean("phone_number_verified"), firstName: text("first_name").notNull(), lastName: text("last_name").notNull(), dob: timestamp("dob").notNull(), rollingNetDepositLimit: integer("rolling_net_deposit_limit").notNull(), rollingNetDepositLimitPeriod: text("rolling_net_deposit_limit_period").notNull(), isLocked: boolean("is_locked").notNull(), }); ``` Expected behaviour ``` export const user = pgTable("user", { id: text("id").primaryKey(), name: text("name").notNull(), email: text("email").notNull().unique(), emailVerified: boolean("email_verified").notNull(), image: text("image"), createdAt: timestamp("created_at").notNull(), updatedAt: timestamp("updated_at").notNull(), phoneNumber: text("phone_number").unique(), phoneNumberVerified: boolean("phone_number_verified"), firstName: text("first_name").notNull(), lastName: text("last_name").notNull(), dob: timestamp("dob").notNull(), rollingNetDepositLimit: integer("rolling_net_deposit_limit").notNull().default(160000), rollingNetDepositLimitPeriod: text("rolling_net_deposit_limit_period").notNull().default("30_days"), isLocked: boolean("is_locked").notNull().default(false), }); ``` ### What version of Better Auth are you using? 1.1.15 ### Provide environment information ```bash - OS: macOS (Sequoia 15.1.1) - browser: Arc ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth"; import { drizzleAdapter } from "better-auth/adapters/drizzle"; import { APIError, createAuthMiddleware } from "better-auth/api"; import { phoneNumber } from "better-auth/plugins"; import { db } from "./db"; import * as schema from "./schema"; export const auth = betterAuth({ baseURL: process.env.VITE_BASE_URL, appName: "Edify", database: drizzleAdapter(db, { provider: "pg", schema: { ...schema }, }), emailAndPassword: { enabled: true, }, // custom model user: { additionalFields: { firstName: { type: "string", required: true, fieldName: "first_name", }, lastName: { type: "string", required: true, fieldName: "last_name", }, dob: { type: "date", required: true, fieldName: "dob", }, rollingNetDepositLimit: { type: "number", required: true, fieldName: "rolling_net_deposit_limit", defaultValue: 160000, input: false, }, rollingNetDepositLimitPeriod: { type: "string", required: true, fieldName: "rolling_net_deposit_limit_period", defaultValue: "30_days", input: false, }, isLocked: { type: "boolean", required: true, fieldName: "is_locked", input: false, defaultValue: false, }, }, }, // hooks databaseHooks: { user: { create: { before: async (user) => { console.log("🚀 ~ before: ~ user:", user); return { data: { ...user, }, }; }, after: async (user) => { // create user on stripe here console.log("🚀 ~ after: ~ user:", user); }, }, }, }, hooks: { before: createAuthMiddleware(async (ctx) => { if (ctx.path !== "/sign-up/email") { return; } }), }, plugins: [ phoneNumber({ sendOTP: async ({ code, phoneNumber }) => { console.log("🚀 ~ sendOTP ~ ctx:", code, phoneNumber); }, }), ], }); ``` ### Additional context I first created this on discord: `https://discord.com/channels/1288403910284935179/1335917057442713620` I am also using Tanstack Start
GiteaMirror added the lockedbug labels 2026-04-13 03:51:55 -05:00
Author
Owner

@razdacoder commented on GitHub (Feb 3, 2025):

@MarkForLoop
Since a default value is already set and the value isn't being explicitly passed when calling the auth client, the required: true property is unnecessary and should be removed.

<!-- gh-comment-id:2631158824 --> @razdacoder commented on GitHub (Feb 3, 2025): @MarkForLoop Since a default value is already set and the value isn't being explicitly passed when calling the auth client, the required: true property is unnecessary and should be removed.
Author
Owner

@MarkForLoop commented on GitHub (Feb 3, 2025):

@razdacoder
Removing the required would make the types be undefined, when a default value is being applied

      isLocked: {
        type: "boolean",
        // required: true, << no longer required
        fieldName: "is_locked",
        input: false,
        defaultValue: false,
      },

Image

<!-- gh-comment-id:2631171806 --> @MarkForLoop commented on GitHub (Feb 3, 2025): @razdacoder Removing the required would make the types be undefined, when a default value is being applied ``` isLocked: { type: "boolean", // required: true, << no longer required fieldName: "is_locked", input: false, defaultValue: false, }, ``` ![Image](https://github.com/user-attachments/assets/94b1eedf-b9d9-43a1-aa1d-053b34325ffd)
Author
Owner

@razdacoder commented on GitHub (Feb 3, 2025):

@MarkForLoop Can you add it manually

<!-- gh-comment-id:2631190403 --> @razdacoder commented on GitHub (Feb 3, 2025): @MarkForLoop Can you add it manually
Author
Owner

@MarkForLoop commented on GitHub (Feb 3, 2025):

Ya I guess I could could do it that way

<!-- gh-comment-id:2631223129 --> @MarkForLoop commented on GitHub (Feb 3, 2025): Ya I guess I could could do it that way
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8701