[GH-ISSUE #2098] before databaseHooks for creating objects do not respect returned object #9051

Closed
opened 2026-04-13 04:20:20 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @lunarW1TCH on GitHub (Apr 3, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/2098

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

in betterAuth method create a database hook for creating a new user
return data without some of the default fields

  databaseHooks: {
    user: {
      create: {
        before: async (user) => {   
          const { image, id, ...rest } = user;

          return {
            data: {
              ...rest,
            },
          };
        },
      },
    },
  },

Current vs. Expected behavior

As in the docs:

Behavior: If the hook returns false, the operation will be aborted. And If it returns a data object, it'll replace the orginal payload.

It works properly for adding new fields or modifying existing ones however omitting fields like image makes better-auth return it anyway leading to errors like:

Unknown argument `image`. Did you mean `name`? Available options are marked with ?.
2025-04-03T08:10:31.019Z ERROR [Better Auth]: unable_to_create_user

first line is a prisma adapter error, i do not have an image field in my db schema

What works fine is specifing explicitly image field as undefined

  databaseHooks: {
    user: {
      create: {
        before: async (user) => {   
          const { image, id, ...rest } = user;

          return {
            data: {
              ...rest,
              image: undefined,
            },
          };
        },
      },
    },
  },

It is connected to issue #1700

What version of Better Auth are you using?

1.2.5

Provide environment information

- OS: Windows 11
- Browser: Zen
- NodeJS: 22.14.0
- next: 15.2.4 - app router

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

Backend

Auth config (if applicable)


Additional context

It also affects production builds, I am hosting on vercel

Originally created by @lunarW1TCH on GitHub (Apr 3, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/2098 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce in betterAuth method create a database hook for creating a new user return data without some of the default fields ```typescript databaseHooks: { user: { create: { before: async (user) => { const { image, id, ...rest } = user; return { data: { ...rest, }, }; }, }, }, }, ``` ### Current vs. Expected behavior As in the [docs](https://www.better-auth.com/docs/concepts/database#1-before-hook): > Behavior: If the hook returns false, the operation will be aborted. And If it returns a data object, it'll replace the orginal payload. It works properly for adding new fields or modifying existing ones however omitting fields like `image` makes better-auth return it anyway leading to errors like: ``` Unknown argument `image`. Did you mean `name`? Available options are marked with ?. 2025-04-03T08:10:31.019Z ERROR [Better Auth]: unable_to_create_user ``` first line is a prisma adapter error, i do not have an `image` field in my db schema What works fine is specifing **explicitly** `image` field as `undefined` ```typescript databaseHooks: { user: { create: { before: async (user) => { const { image, id, ...rest } = user; return { data: { ...rest, image: undefined, }, }; }, }, }, }, ``` It is connected to issue #1700 ### What version of Better Auth are you using? 1.2.5 ### Provide environment information ```bash - OS: Windows 11 - Browser: Zen - NodeJS: 22.14.0 - next: 15.2.4 - app router ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript ``` ### Additional context It also affects production builds, I am hosting on vercel
GiteaMirror added the lockedbug labels 2026-04-13 04:20:20 -05:00
Author
Owner

@Kinfe123 commented on GitHub (Apr 4, 2025):

how you are actually calling the action like the function that runs on signup ?

<!-- gh-comment-id:2779761171 --> @Kinfe123 commented on GitHub (Apr 4, 2025): how you are actually calling the action like the function that runs on signup ?
Author
Owner

@lunarW1TCH commented on GitHub (Apr 5, 2025):

it runs automatically after signIn action with social provider is called and account with that email does not exist in the database

that's the default behaviour for social providers

<!-- gh-comment-id:2780682415 --> @lunarW1TCH commented on GitHub (Apr 5, 2025): it runs automatically after `signIn` action with social provider is called ***and*** account with that email does not exist in the database that's the default behaviour for social providers
Author
Owner

@Bekacru commented on GitHub (Apr 5, 2025):

The data you return inside a hook will be merged with the original object. So, if you don't overwrite a field, it'd fallback to the original field.

<!-- gh-comment-id:2781022201 --> @Bekacru commented on GitHub (Apr 5, 2025): The data you return inside a hook will be merged with the original object. So, if you don't overwrite a field, it'd fallback to the original field.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#9051