Unable to get bearer token

Following the docs I am unable to get a non-null bearer token when signing in. I sign in fine for the cookie based session, but I need to additionally store the bearer token for later method. Any help appreciated

My better-auth setup:
========================================
import { betterAuth, type BetterAuthOptions } from "better-auth";
import { bearer } from "better-auth/plugins";

export const createBetterAuth = (config: {
database: BetterAuthOptions["database"];
secret?: BetterAuthOptions["secret"];
socialProviders?: BetterAuthOptions["socialProviders"];
}): ReturnType<typeof betterAuth> => {
return betterAuth({
database: config.database,
secret: config.secret,
emailAndPassword: { enabled: false },
socialProviders: config.socialProviders,
plugins: [bearer()],

user: { modelName: "auth_user" },
session: { modelName: "auth_session" },
verification: { modelName: "auth_verification" },
account: { modelName: "auth_account" },
});
};

My auth-client.ts:
========================================
import { createAuthClient } from "better-auth/react";

export const authClient = createAuthClient({
fetchOptions: {
auth: {
type: "Bearer",
token: () => localStorage.getItem("bearer_token") || "",
},
},
});

export const { useSession, signIn, signOut } = authClient;

My login:
========================================
const handleGoogleSignIn = async () => {
await authClient.signIn.social(
{
provider: "google",
callbackURL: "/",
},
{
onSuccess: (ctx) => {
const token = ctx.response.headers.get("set-auth-token");
if (token) {
localStorage.setItem("bearer_token", token);
}
},
onError: (error) => {
console.error("Error signing in", error);
},
}
);
};
Was this page helpful?