[GH-ISSUE #1631] fetchOptions onsuccess not be called when use bearer token plugin. #26167

Closed
opened 2026-04-17 16:38:46 -05:00 by GiteaMirror · 13 comments
Owner

Originally created by @wnbotoo on GitHub (Mar 2, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/1631

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

use bearer token plugin, add this code from doc:

export const authClient = createAuthClient({
    fetchOptions: {
        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){
              localStorage.setItem("bearer_token", authToken);
            }
        }
    }
});

then signin with otp code.

Current vs. Expected behavior

follow the document : https://better-auth.vercel.app/docs/plugins/bearer, use otp code sign, the onSuccess in fetchOptions not be called when sign in success.

also, when use social auth like sign in with google, the onSuccess like below is not be called too:

export const signInWithGoogle = async () => {
  const { data } = await authClient.signIn.social(
    {
      provider: "google",
      callbackURL: "/feed",
    },
    {
      onSuccess: (ctx) => {
        console.log("signInWithGoogle success");
        responseHandler(ctx.response);
      },
    }
  );
};

but onSuccess in authClient.signIn.emailOtp works.

What version of Better Auth are you using?

1.1.21, 1.2.0

Provide environment information

Wsl2-ubuntu24.04

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

Documentation, Client, Backend

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({
  emailAndPassword: {  
    enabled: true
  },
});

Additional context

No response

Originally created by @wnbotoo on GitHub (Mar 2, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/1631 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce use bearer token plugin, add this code from doc: ``` export const authClient = createAuthClient({ fetchOptions: { 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){ localStorage.setItem("bearer_token", authToken); } } } }); ``` then signin with otp code. ### Current vs. Expected behavior follow the document : https://better-auth.vercel.app/docs/plugins/bearer, use otp code sign, the onSuccess in fetchOptions not be called when sign in success. also, when use social auth like sign in with google, the onSuccess like below is not be called too: ``` export const signInWithGoogle = async () => { const { data } = await authClient.signIn.social( { provider: "google", callbackURL: "/feed", }, { onSuccess: (ctx) => { console.log("signInWithGoogle success"); responseHandler(ctx.response); }, } ); }; ``` but `onSuccess` in ` authClient.signIn.emailOtp` works. ### What version of Better Auth are you using? 1.1.21, 1.2.0 ### Provide environment information ```bash Wsl2-ubuntu24.04 ``` ### Which area(s) are affected? (Select all that apply) Documentation, Client, Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, }); ``` ### Additional context _No response_
GiteaMirror added the lockedbug labels 2026-04-17 16:38:46 -05:00
Author
Owner

@dmklsv commented on GitHub (Mar 25, 2025):

I confirm. The same problem with the github provider, I can't get the authorization token for the baerer plugin in the onSucess hook to put it in localStorage
I'm developing an application on Tauri with Better-Auth 1.2.4

<!-- gh-comment-id:2749893880 --> @dmklsv commented on GitHub (Mar 25, 2025): I confirm. The same problem with the github provider, I can't get the authorization token for the baerer plugin in the onSucess hook to put it in localStorage I'm developing an application on Tauri with Better-Auth 1.2.4
Author
Owner

@BjoernRave commented on GitHub (Mar 25, 2025):

same for me. I am not sure when signing in with social how to get the bearer token to store it.

<!-- gh-comment-id:2752789204 --> @BjoernRave commented on GitHub (Mar 25, 2025): same for me. I am not sure when signing in with social how to get the bearer token to store it.
Author
Owner

@wsxiaoys commented on GitHub (Apr 24, 2025):

As workaround, i found onResponse is invoked when going through social login flow and set-auth-token can be properly extracted.

<!-- gh-comment-id:2826270811 --> @wsxiaoys commented on GitHub (Apr 24, 2025): As workaround, i found `onResponse` is invoked when going through social login flow and `set-auth-token` can be properly extracted.
Author
Owner

@datgrog commented on GitHub (May 5, 2025):

same for me, can confirm 👍

<!-- gh-comment-id:2850809550 --> @datgrog commented on GitHub (May 5, 2025): same for me, can confirm 👍
Author
Owner

@asrvd commented on GitHub (May 19, 2025):

can confirm, onSuccess callback work when using directly in the sign-in method but doesn't run when configured globally

<!-- gh-comment-id:2890422049 --> @asrvd commented on GitHub (May 19, 2025): can confirm, `onSuccess` callback work when using directly in the sign-in method but doesn't run when configured globally
Author
Owner

@yovanoc commented on GitHub (May 25, 2025):

can confirm too

<!-- gh-comment-id:2907760632 --> @yovanoc commented on GitHub (May 25, 2025): can confirm too
Author
Owner

@martin-dimi commented on GitHub (May 28, 2025):

Same here

<!-- gh-comment-id:2917628267 --> @martin-dimi commented on GitHub (May 28, 2025): Same here
Author
Owner

@TheMcnafaha commented on GitHub (Jun 9, 2025):

any updates on this behavior? I heard it was the result of an internal callback but could not find any workarounds.

<!-- gh-comment-id:2954619556 --> @TheMcnafaha commented on GitHub (Jun 9, 2025): any updates on this behavior? I heard it was the result of an internal callback but could not find any workarounds.
Author
Owner

@TheMcnafaha commented on GitHub (Jun 9, 2025):

for anyone interested, I think the best workaround is to use callBackurl. link to relevant repo https://github.com/better-auth/better-auth/issues/1555

<!-- gh-comment-id:2954634989 --> @TheMcnafaha commented on GitHub (Jun 9, 2025): for anyone interested, I think the best workaround is to use callBackurl. link to relevant repo https://github.com/better-auth/better-auth/issues/1555
Author
Owner

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

can you try this one

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

and check if it works ?

<!-- gh-comment-id:2973530665 --> @Kinfe123 commented on GitHub (Jun 15, 2025): can you try this one ```node npm i https://pkg.pr.new/better-auth/better-auth@3032 ``` and check if it works ?
Author
Owner

@TheMcnafaha commented on GitHub (Jun 16, 2025):

I have tried. Unfortunately, the onSuccess callback is called the moment you hit the provider login page and not if the provider login succeeded.

Should I send my system info? @Kinfe123

<!-- gh-comment-id:2975142349 --> @TheMcnafaha commented on GitHub (Jun 16, 2025): I have tried. Unfortunately, the onSuccess callback is called the moment you hit the provider login page and not if the provider login succeeded. Should I send my system info? @Kinfe123
Author
Owner

@baptisteArno commented on GitHub (Feb 2, 2026):

It's not fixed 🤔

The set-auth-token header exists on GET /api/auth/callback/github (302). That’s the response that sets the session cookie, so the bearer plugin adds set-auth-token. So the callback is never called.

bearer plugin just does not work out of the box for social sign in

<!-- gh-comment-id:3835553121 --> @baptisteArno commented on GitHub (Feb 2, 2026): It's not fixed 🤔 The `set-auth-token` header exists on GET /api/auth/callback/github (302). That’s the response that sets the session cookie, so the bearer plugin adds set-auth-token. So the callback is never called. bearer plugin just does not work out of the box for social sign in
Author
Owner

@github-actions[bot] commented on GitHub (Apr 1, 2026):

This issue has been locked as it was closed more than 7 days ago. If you're experiencing a similar problem or you have additional context, please open a new issue and reference this one.

<!-- gh-comment-id:4166561196 --> @github-actions[bot] commented on GitHub (Apr 1, 2026): This issue has been locked as it was closed more than 7 days ago. If you're experiencing a similar problem or you have additional context, please open a new issue and reference this one.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#26167