[GH-ISSUE #1422] better errors #8745

Closed
opened 2026-04-13 03:56:21 -05:00 by GiteaMirror · 5 comments
Owner

Originally created by @juliusmarminge on GitHub (Feb 12, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/1422

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

i... don't know. all i see is a generic error. doens't say where it was thrown or why

Current vs. Expected behavior

i am getting this generic error:

[APIError [BetterCallAPIError]: API Error: UNAUTHORIZED ] {
  status: 'UNAUTHORIZED',
  headers: Headers {},
  body: { code: 'UNAUTHORIZED' },
  [cause]: undefined
}

doesn't give me much to go on. what's unauthorized? where was this error thrown?

What version of Better Auth are you using?

1.1.17

Provide environment information

latest macos
tanstack start

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

Backend

Auth config (if applicable)

export const auth = betterAuth({
  database: drizzleAdapter(db, {
    provider: "pg",
    schema: authTables,
    usePlural: true,
  }),
  emailAndPassword: {
    enabled: true,
  },
  plugins: [organization(), admin(), openAPI()],
});

Additional context

sorry for a very vague report but I honestly don't know where to start debugging this... tried giving a search where this error is thrown in the repo and there are some places you throw errors only with the code and no explanation. i'm using the backend api so don't think it's due to redacting sensitive info.

Originally created by @juliusmarminge on GitHub (Feb 12, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/1422 ### Is this suited for github? - [ ] Yes, this is suited for github ### To Reproduce i... don't know. all i see is a generic error. doens't say where it was thrown or why ### Current vs. Expected behavior i am getting this generic error: ``` [APIError [BetterCallAPIError]: API Error: UNAUTHORIZED ] { status: 'UNAUTHORIZED', headers: Headers {}, body: { code: 'UNAUTHORIZED' }, [cause]: undefined } ``` doesn't give me much to go on. what's unauthorized? where was this error thrown? ### What version of Better Auth are you using? 1.1.17 ### Provide environment information ```bash latest macos tanstack start ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript export const auth = betterAuth({ database: drizzleAdapter(db, { provider: "pg", schema: authTables, usePlural: true, }), emailAndPassword: { enabled: true, }, plugins: [organization(), admin(), openAPI()], }); ``` ### Additional context sorry for a very vague report but I honestly don't know where to start debugging this... tried giving a search where this error is thrown in the repo and there are some places you throw errors only with the code and no explanation. i'm using the backend api so don't think it's due to redacting sensitive info.
GiteaMirror added the lockedbug labels 2026-04-13 03:56:22 -05:00
Author
Owner

@Bekacru commented on GitHub (Feb 12, 2025):

The error occurs because you're calling the api.auth method on the server. We should add the exact path or action that triggered the error, but it should still be debuggable since it only happens when an action is attempted. The error is coming from an internal auth check middleware (that's why it's generic), so I assume you're not passing headers to an action that requires a session.

<!-- gh-comment-id:2652714545 --> @Bekacru commented on GitHub (Feb 12, 2025): The error occurs because you're calling the `api.auth` method on the server. We should add the exact path or action that triggered the error, but it should still be debuggable since it only happens when an action is attempted. The error is coming from an internal auth check middleware (that's why it's generic), so I assume you're not passing `headers` to an action that requires a session.
Author
Owner

@juliusmarminge commented on GitHub (Feb 12, 2025):

So I did some more debugging and adding some catch clauses and it seems to be ffrom api.listOrganizations and that afaict doesn't take headers ?

Image

Image

<!-- gh-comment-id:2653078972 --> @juliusmarminge commented on GitHub (Feb 12, 2025): So I did some more debugging and adding some catch clauses and it seems to be ffrom `api.listOrganizations` and that afaict doesn't take `headers` ? ![Image](https://github.com/user-attachments/assets/6929f479-7df5-4fc2-8137-f2afb3441b7b) ![Image](https://github.com/user-attachments/assets/4336e3bc-e003-4cae-9dce-7020f141d38e)
Author
Owner

@Bekacru commented on GitHub (Feb 12, 2025):

It still accepts headers if provided. We should probably enforce headers at the type level for this endpoint, but I’d still recommend avoiding auth.api for these kinds of actions, at least for now. It re-fetches the session to verify auth, which is unnecessary if you’ve already done that beforehand. If you're not calling it from the client, you can directly call the ORM to list organizations instead (since that's all its doing). That’s why it’s not documented in the org docs. That said, we're reworking this to allow these actions without requiring a session refetch.

<!-- gh-comment-id:2653133258 --> @Bekacru commented on GitHub (Feb 12, 2025): It still accepts `headers` if provided. We should probably enforce headers at the type level for this endpoint, but I’d still recommend avoiding `auth.api` for these kinds of actions, at least for now. It re-fetches the session to verify auth, which is unnecessary if you’ve already done that beforehand. If you're not calling it from the client, you can directly call the ORM to list organizations instead (since that's all its doing). That’s why it’s not documented in the org docs. That said, we're reworking this to allow these actions without requiring a session refetch.
Author
Owner

@juliusmarminge commented on GitHub (Feb 12, 2025):

oh ok. that's what i sort of ended up doing, just raw-dogging the db requests myself 😅 glad to know that's the recommended way

<!-- gh-comment-id:2653185904 --> @juliusmarminge commented on GitHub (Feb 12, 2025): oh ok. that's what i sort of ended up doing, just raw-dogging the db requests myself 😅 glad to know that's the recommended way
Author
Owner

@emileindik commented on GitHub (Oct 8, 2025):

Hey any progress on "reworking this to allow these actions without requiring a session refetch"?
Passing headers works fine when my client is coming from the browser, but it breaks when I have clients authenticated via api token, which I authenticate outside of better-auth.

Thanks!

<!-- gh-comment-id:3379502845 --> @emileindik commented on GitHub (Oct 8, 2025): Hey any progress on "reworking this to allow these actions without requiring a session refetch"? Passing headers works fine when my client is coming from the browser, but it breaks when I have clients authenticated via api token, which I authenticate outside of better-auth. Thanks!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8745