[GH-ISSUE #2008] [Bearer] Support async fn for token #9001

Closed
opened 2026-04-13 04:15:19 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @BjoernRave on GitHub (Mar 26, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/2008

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

Use an async function in the auth client to retrieve the bearer token, so it gets added to every request.

Current vs. Expected behavior

I expect the fn to get resolved and add the result of the promise to the request, but currently I get: authorization Bearer [object Promise]

What version of Better Auth are you using?

1.2.4

Provide environment information

- MacOS, but probably not relevant here

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

Client

Auth config (if applicable)

export const authClient = createAuthClient({
  plugins: [anonymousClient(), emailOTPClient()],

  fetchOptions: {
    baseURL: `${process.env.NEXT_PUBLIC_DASHBOARD_URL}/api/auth`,
    onError(e) {
      if (e.error.status === 429) {
        toast.error("Too many requests. Please try again later.")
      }
    },

    onSuccess: (ctx) => {
      const authToken = ctx.response.headers.get("set-auth-token") // get the token from the response headers
      // Store the token securely (e.g., in localStorage)
      if (authToken) {
        Preferences.set({
          key: AUTH_TOKEN,
          value: authToken,
        })
      }
    },
    auth: {
      type: "Bearer",

      token: async () => {
        const token = await Preferences.get({ key: AUTH_TOKEN })
      },
    },
  },
})

Additional context

No response

Originally created by @BjoernRave on GitHub (Mar 26, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/2008 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce Use an async function in the auth client to retrieve the bearer token, so it gets added to every request. ### Current vs. Expected behavior I expect the fn to get resolved and add the result of the promise to the request, but currently I get: `authorization Bearer [object Promise]` ### What version of Better Auth are you using? 1.2.4 ### Provide environment information ```bash - MacOS, but probably not relevant here ``` ### Which area(s) are affected? (Select all that apply) Client ### Auth config (if applicable) ```typescript export const authClient = createAuthClient({ plugins: [anonymousClient(), emailOTPClient()], fetchOptions: { baseURL: `${process.env.NEXT_PUBLIC_DASHBOARD_URL}/api/auth`, onError(e) { if (e.error.status === 429) { toast.error("Too many requests. Please try again later.") } }, onSuccess: (ctx) => { const authToken = ctx.response.headers.get("set-auth-token") // get the token from the response headers // Store the token securely (e.g., in localStorage) if (authToken) { Preferences.set({ key: AUTH_TOKEN, value: authToken, }) } }, auth: { type: "Bearer", token: async () => { const token = await Preferences.get({ key: AUTH_TOKEN }) }, }, }, }) ``` ### Additional context _No response_
GiteaMirror added the lockedbug labels 2026-04-13 04:15:19 -05:00
Author
Owner

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

it is already been fixed

<!-- gh-comment-id:2798766147 --> @Kinfe123 commented on GitHub (Apr 12, 2025): it is already been fixed
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#9001