BA
Better Auth•2d ago
vz

Global onSuccess Hook does not work in auth-client

I am setting up bearer token following https://www.better-auth.com/docs/plugins/bearer My code:
import { env } from "@/env";
import { createAuthClient } from "better-auth/react";
import { magicLinkClient } from "better-auth/client/plugins";
export const authClient = createAuthClient({
baseURL: env.NEXT_PUBLIC_BETTER_AUTH_URL,
plugins: [magicLinkClient()],
fetchOptions: {
onSuccess: (ctx) => {
console.debug("Auth client response headers", ctx.response.headers);
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) {
console.debug("🔑 Auth Token Set", authToken);
localStorage.setItem("bearer_token", authToken);
}
},
},
});
export const { signIn, signUp, useSession, signOut, magicLink } = authClient;
import { env } from "@/env";
import { createAuthClient } from "better-auth/react";
import { magicLinkClient } from "better-auth/client/plugins";
export const authClient = createAuthClient({
baseURL: env.NEXT_PUBLIC_BETTER_AUTH_URL,
plugins: [magicLinkClient()],
fetchOptions: {
onSuccess: (ctx) => {
console.debug("Auth client response headers", ctx.response.headers);
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) {
console.debug("🔑 Auth Token Set", authToken);
localStorage.setItem("bearer_token", authToken);
}
},
},
});
export const { signIn, signUp, useSession, signOut, magicLink } = authClient;
i dont see Auth client response headers in the logs
Bearer Token Authentication | Better Auth
Authenticate API requests using Bearer tokens instead of browser cookies
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?