[GH-ISSUE #1884] API Key Plugin: Error reading model from schema #8961

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

Originally created by @ismael-iskauskas on GitHub (Mar 19, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/1884

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

If you change the plugin schema to anything different from apikey the kysely adapter can not find the schema.

the reason for this is:
the schema is defined with the key apikey in file packages/better-auth/src/plugins/api-key/schema.ts

export const apiKeySchema = ({
  timeWindow,
  rateLimitMax,
}: { timeWindow: number; rateLimitMax: number }) =>
  ({
    apikey: {
      modelName: "apikey",
      fields: {
....

but it is in the endpoints it is used with schema.apikey.modelName
example:

packages/better-auth/src/plugins/api-key/routes/create-api-key.ts line 281 to 284

const apiKey = await ctx.context.adapter.create<ApiKey>({
  model: schema.apikey.modelName,
  data: data,
});

Current vs. Expected behavior

it should work that you can change the modelName in schema of ApiKey plugin.

the fix could be easy implemented if done like in twoFactor plugin

What version of Better Auth are you using?

1.2.4

Provide environment information

Does not matter in this context

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

Client, Other

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({
  emailAndPassword: {  
    enabled: true
  },
plugins: [
      openAPI({}),
      admin(),
      apiKey({
        schema: {
          apikey: {
            modelName: 'auth.api_keys',
            fields: {

            },
          },
        },
      }),
      twoFactor({
        schema: {
          twoFactor: {
            modelName: 'auth.two_factors',
            fields: {},
          },
        },
      }),
    ],
});

Additional context

No response

Originally created by @ismael-iskauskas on GitHub (Mar 19, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/1884 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce If you change the plugin schema to anything different from apikey the kysely adapter can not find the schema. the reason for this is: the schema is defined with the key apikey in file packages/better-auth/src/plugins/api-key/schema.ts ``` ts export const apiKeySchema = ({ timeWindow, rateLimitMax, }: { timeWindow: number; rateLimitMax: number }) => ({ apikey: { modelName: "apikey", fields: { .... ``` but it is in the endpoints it is used with schema.apikey.modelName example: packages/better-auth/src/plugins/api-key/routes/create-api-key.ts line 281 to 284 ``` ts const apiKey = await ctx.context.adapter.create<ApiKey>({ model: schema.apikey.modelName, data: data, }); ``` ### Current vs. Expected behavior it should work that you can change the modelName in schema of ApiKey plugin. the fix could be easy implemented if done like in twoFactor plugin ### What version of Better Auth are you using? 1.2.4 ### Provide environment information ```bash Does not matter in this context ``` ### Which area(s) are affected? (Select all that apply) Client, Other ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, plugins: [ openAPI({}), admin(), apiKey({ schema: { apikey: { modelName: 'auth.api_keys', fields: { }, }, }, }), twoFactor({ schema: { twoFactor: { modelName: 'auth.two_factors', fields: {}, }, }, }), ], }); ``` ### Additional context _No response_
GiteaMirror added the lockedbug labels 2026-04-13 04:12:31 -05:00
Author
Owner

@florianmartens commented on GitHub (Mar 20, 2025):

Thanks for reporting this. This affects me too.

<!-- gh-comment-id:2739982173 --> @florianmartens commented on GitHub (Mar 20, 2025): Thanks for reporting this. This affects me too.
Author
Owner

@ismael-iskauskas commented on GitHub (Mar 20, 2025):

Already created an pullrequest

<!-- gh-comment-id:2740196298 --> @ismael-iskauskas commented on GitHub (Mar 20, 2025): Already created an pullrequest
Author
Owner

@Kinfe123 commented on GitHub (May 3, 2025):

apikey is not currently a part of the core schema to mod and customize the table name on the plugin itself

<!-- gh-comment-id:2848862051 --> @Kinfe123 commented on GitHub (May 3, 2025): `apikey` is not currently a part of the core schema to mod and customize the table name on the plugin itself
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8961