adminPlugin not working properly when useNumberId is set true #1470

Closed
opened 2026-03-13 08:41:58 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @szilardx on GitHub (Jul 6, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. Using Next.js , and mariadb with this config:
export const auth = betterAuth({
  database: pool,
  advanced: {
    database: {
      useNumberId: true,
    },
  },
  plugins: [
    nextCookies(),
    adminPlugin({
      ac,
      roles: {
        admin,
        user,
      },
    }),
  ],
 ...

Calling this:

await auth.api.userHasPermission({
    body: {
      role: role,
      permissions: {
        user: ["list", "create", "delete"],
      },
    },
  })

Results in this:

 ⨯ Error: Unknown column 'NaN' in 'WHERE'
    at async Layout (src/app/(dashboard)/(admin)/users/layout.tsx:21:40)
  19 |   const role: Role = session?.user.role === "admin" ? "admin" : "user";
  20 |
> 21 |   const canManageUsersAndAdminRequest = await auth.api.userHasPermission({
     |                                        ^
  22 |     body: {
  23 |       role: role,
  24 |       permissions: { {
  code: 'ER_BAD_FIELD_ERROR',
  errno: 1054,
  sqlState: '42S22',
  sqlMessage: "Unknown column 'NaN' in 'WHERE'",
  sql: 'select * from `user` where `id` = NaN',
  digest: '3645066394'
}

Current vs. Expected behavior

I used the api.userHasPermission call from adminPlugin successfully with mariadb. Then I had the requirement to switch to numeric ids. This caused the error described above. Debugging showed that session.user.id is a number, in string format (ie: '54322').

What version of Better Auth are you using?

1.2.12

Provide environment information

- OS: MacOS 15.5
- latest Firefox 140.0.2

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

Backend

Auth config (if applicable)


Additional context

No response

Originally created by @szilardx on GitHub (Jul 6, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1. Using Next.js , and mariadb with this config: ``` export const auth = betterAuth({ database: pool, advanced: { database: { useNumberId: true, }, }, plugins: [ nextCookies(), adminPlugin({ ac, roles: { admin, user, }, }), ], ... ``` Calling this: ``` await auth.api.userHasPermission({ body: { role: role, permissions: { user: ["list", "create", "delete"], }, }, }) ``` Results in this: ``` ⨯ Error: Unknown column 'NaN' in 'WHERE' at async Layout (src/app/(dashboard)/(admin)/users/layout.tsx:21:40) 19 | const role: Role = session?.user.role === "admin" ? "admin" : "user"; 20 | > 21 | const canManageUsersAndAdminRequest = await auth.api.userHasPermission({ | ^ 22 | body: { 23 | role: role, 24 | permissions: { { code: 'ER_BAD_FIELD_ERROR', errno: 1054, sqlState: '42S22', sqlMessage: "Unknown column 'NaN' in 'WHERE'", sql: 'select * from `user` where `id` = NaN', digest: '3645066394' } ``` ### Current vs. Expected behavior I used the api.userHasPermission call from adminPlugin successfully with mariadb. Then I had the requirement to switch to numeric ids. This caused the error described above. Debugging showed that session.user.id is a number, in string format (ie: '54322'). ### What version of Better Auth are you using? 1.2.12 ### Provide environment information ```bash - OS: MacOS 15.5 - latest Firefox 140.0.2 ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript ``` ### Additional context _No response_
GiteaMirror added the stalebug labels 2026-03-13 08:41:58 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Jul 6, 2025):

Hi @szilardx! I'm Dosu and I’m helping the better-auth team.

This is a known bug when using adminPlugin with useNumberId enabled: the system sometimes fails to convert string user IDs to numbers before building SQL queries, which leads to errors like Unknown column 'NaN' in 'WHERE' when a string (e.g., '54322') is passed instead of a number. This happens because, even with useNumberId set, session or API code may still provide IDs as strings, and the plugin/API doesn't always coerce them to numbers as required by the database schema. The bug has been acknowledged by maintainers and discussed in related issues and PRs (example).

As a workaround, make sure to explicitly convert any user ID to a number before passing it to Better Auth API calls or plugins when useNumberId is true. For example, if you get the ID from the session as a string, use Number(session.user.id) before using it in permission checks or queries. This should prevent the NaN error until a more robust fix is released.

Recent PRs have improved numeric ID support, but full type safety and conversion coverage for all plugins (including adminPlugin) may still be incomplete (PR 1976, PR 3228). Keep an eye on updates for more comprehensive fixes.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Join Discord Share on X

@dosubot[bot] commented on GitHub (Jul 6, 2025): <!-- Greeting --> Hi @szilardx! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> This is a known bug when using adminPlugin with useNumberId enabled: the system sometimes fails to convert string user IDs to numbers before building SQL queries, which leads to errors like `Unknown column 'NaN' in 'WHERE'` when a string (e.g., `'54322'`) is passed instead of a number. This happens because, even with useNumberId set, session or API code may still provide IDs as strings, and the plugin/API doesn't always coerce them to numbers as required by the database schema. The bug has been acknowledged by maintainers and discussed in related issues and PRs ([example](https://github.com/better-auth/better-auth/issues/3190)). As a workaround, make sure to explicitly convert any user ID to a number before passing it to Better Auth API calls or plugins when useNumberId is true. For example, if you get the ID from the session as a string, use `Number(session.user.id)` before using it in permission checks or queries. This should prevent the NaN error until a more robust fix is released. Recent PRs have improved numeric ID support, but full type safety and conversion coverage for all plugins (including adminPlugin) may still be incomplete ([PR 1976](https://github.com/better-auth/better-auth/pull/1976), [PR 3228](https://github.com/better-auth/better-auth/pull/3228)). Keep an eye on updates for more comprehensive fixes. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/bc919437-8704-4b65-8620-084812a75d69?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/bc919437-8704-4b65-8620-084812a75d69?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/bc919437-8704-4b65-8620-084812a75d69?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/bc919437-8704-4b65-8620-084812a75d69?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/bc919437-8704-4b65-8620-084812a75d69?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/bc919437-8704-4b65-8620-084812a75d69?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/bc919437-8704-4b65-8620-084812a75d69?feedback_type=other)</sup>&nbsp;&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/3282)
Author
Owner

@ping-maxwell commented on GitHub (Oct 7, 2025):

@szilardx Can you confirm this is still an issue on latest? I noticed some code changes since then that could suggest this

@ping-maxwell commented on GitHub (Oct 7, 2025): @szilardx Can you confirm this is still an issue on latest? I noticed some code changes since then that could suggest this
Author
Owner

@dosubot[bot] commented on GitHub (Jan 6, 2026):

Hi, @szilardx. I'm Dosu, and I'm helping the better-auth team manage their backlog and am marking this issue as stale.

Issue Summary:

  • You reported that in betterAuth v1.2.12 with MariaDB and useNumberId=true, the adminPlugin's api.userHasPermission fails due to a SQL error caused by user ID being treated as NaN.
  • It was identified as a known bug where string IDs are not properly converted to numbers in queries.
  • A suggested workaround was to explicitly convert IDs to numbers before making API calls.
  • The maintainer asked you to check if this issue still occurs on the latest version, as recent changes might have fixed it.

Next Steps:

  • Please confirm if this issue is still relevant with the latest version of better-auth by commenting here.
  • If I do not hear back within 7 days, I will automatically close this issue.

Thank you for your understanding and contribution!

@dosubot[bot] commented on GitHub (Jan 6, 2026): Hi, @szilardx. I'm [Dosu](https://dosu.dev), and I'm helping the better-auth team manage their backlog and am marking this issue as stale. **Issue Summary:** - You reported that in betterAuth v1.2.12 with MariaDB and `useNumberId=true`, the `adminPlugin`'s `api.userHasPermission` fails due to a SQL error caused by user ID being treated as NaN. - It was identified as a known bug where string IDs are not properly converted to numbers in queries. - A suggested workaround was to explicitly convert IDs to numbers before making API calls. - The maintainer asked you to check if this issue still occurs on the latest version, as recent changes might have fixed it. **Next Steps:** - Please confirm if this issue is still relevant with the latest version of better-auth by commenting here. - If I do not hear back within 7 days, I will automatically close this issue. Thank you for your understanding and contribution!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1470