auth.api.userHasPermission method does throw error if no user is provided #863

Closed
opened 2026-03-13 08:07:35 -05:00 by GiteaMirror · 5 comments
Owner

Originally created by @jonaspm on GitHub (Mar 17, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

Docs mention that userHasPermission can get userId or role but the following code throws error:

await auth.api.userHasPermission({
    body: {
        role,
        permission: { raffle: [action] },
    },
});

error:

Image

Current vs. Expected behavior

current behaviour: execution error
expected behaviour: permissioncheck result

What version of Better Auth are you using?

1.2.4

Provide environment information

- OS: Windows 11 x64 
- Browser: Firefox v136.0.1
- Runtime: Bun

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

Backend

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({
  plugins: [
      adminPlugin({
          ac: ac,
          roles: {
              admin,
              user
          }
      }),
  ]
  emailAndPassword: {  
    enabled: true
  },
});

Additional context

permissions.ts

import { createAccessControl } from "better-auth/plugins/access";
import { defaultStatements, adminAc } from "better-auth/plugins/admin/access";

const customDefaultStatements = Object.freeze({
    user: Array.from(new Set([...defaultStatements.user, "update"])),
    session: defaultStatements.session,
});

const statement = {
    ...customDefaultStatements, 
    raffle: [ "create", "list", "update", "delete" ],
} as const;
 
export const ac = createAccessControl(statement);
 
export const admin = ac.newRole({
    raffle: [ "create", "update", "delete", "list" ],
    ...adminAc.statements, 
});

export const user = ac.newRole({
    raffle: [ "create", "update", "delete", "list" ],
    user: [ "update", "list" ],
});
```
Originally created by @jonaspm on GitHub (Mar 17, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce Docs mention that userHasPermission can get userId or role but the following code throws error: ```javascript await auth.api.userHasPermission({ body: { role, permission: { raffle: [action] }, }, }); ``` error: ![Image](https://github.com/user-attachments/assets/028cea87-4a16-4d97-8fcb-127ca0182c5b) ### Current vs. Expected behavior current behaviour: execution error expected behaviour: permissioncheck result ### What version of Better Auth are you using? 1.2.4 ### Provide environment information ```bash - OS: Windows 11 x64 - Browser: Firefox v136.0.1 - Runtime: Bun ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ plugins: [ adminPlugin({ ac: ac, roles: { admin, user } }), ] emailAndPassword: { enabled: true }, }); ``` ### Additional context permissions.ts ````javascript import { createAccessControl } from "better-auth/plugins/access"; import { defaultStatements, adminAc } from "better-auth/plugins/admin/access"; const customDefaultStatements = Object.freeze({ user: Array.from(new Set([...defaultStatements.user, "update"])), session: defaultStatements.session, }); const statement = { ...customDefaultStatements, raffle: [ "create", "list", "update", "delete" ], } as const; export const ac = createAccessControl(statement); export const admin = ac.newRole({ raffle: [ "create", "update", "delete", "list" ], ...adminAc.statements, }); export const user = ac.newRole({ raffle: [ "create", "update", "delete", "list" ], user: [ "update", "list" ], }); ```
GiteaMirror added the bug label 2026-03-13 08:07:35 -05:00
Author
Owner

@szilardx commented on GitHub (May 28, 2025):

According to the signature auth.api.userHasPermission has to accept an undefined role. But if I pass undefined as a role, it throws APIError: Invalid body parameters.

@szilardx commented on GitHub (May 28, 2025): According to the signature auth.api.userHasPermission has to accept an undefined role. But if I pass undefined as a role, it throws APIError: Invalid body parameters.
Author
Owner

@takeshikriang commented on GitHub (Jun 3, 2025):

I ran into the same issue and have to use userId instead of role

const results = await auth.api.userHasPermission({
  body: {
    userId: 'my_user_id',
    permissions: {
      user: ['create']
    }
  }
});
@takeshikriang commented on GitHub (Jun 3, 2025): I ran into the same issue and have to use `userId` instead of `role` ``` const results = await auth.api.userHasPermission({ body: { userId: 'my_user_id', permissions: { user: ['create'] } } }); ```
Author
Owner

@Kinfe123 commented on GitHub (Jun 13, 2025):

Can you please update Better Auth to the latest version and see if you run into this issue?

@Kinfe123 commented on GitHub (Jun 13, 2025): Can you please update Better Auth to the latest version and see if you run into this issue?
Author
Owner

@Kinfe123 commented on GitHub (Jun 13, 2025):

alternatively please check by installing this

npm i https://pkg.pr.new/better-auth/better-auth@3018

i have added additional guard on it. and let me know if it works.

@Kinfe123 commented on GitHub (Jun 13, 2025): alternatively please check by installing this ``` npm i https://pkg.pr.new/better-auth/better-auth@3018 ``` i have added additional guard on it. and let me know if it works.
Author
Owner

@ping-maxwell commented on GitHub (Jul 1, 2025):

It's been 2-3 weeks now, given no response I'll close this.

@ping-maxwell commented on GitHub (Jul 1, 2025): It's been 2-3 weeks now, given no response I'll close this.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#863