diff --git a/src/lib/auth-client.ts b/src/lib/auth-client.ts index a6fe18f..3613ed4 100644 --- a/src/lib/auth-client.ts +++ b/src/lib/auth-client.ts @@ -1,8 +1,36 @@ import { createAuthClient } from "better-auth/react" +import type { BetterFetchPlugin } from "@better-fetch/fetch" + +/** + * Maps 'name' -> 'display_name' in register requests to match the API's RegisterRequest schema. + */ +const displayNameMapper: BetterFetchPlugin = { + id: "display-name-mapper", + name: "display-name-mapper", + hooks: { + onRequest: async (context) => { + const url = typeof context.url === "string" ? context.url : context.url.pathname + if ( + url.endsWith("/auth/register") && + context.method === "POST" && + context.body && + "name" in context.body + ) { + context.body = { + ...context.body, + display_name: context.body.name as string, + name: undefined, + } + } + return context + }, + }, +} export const authClient = createAuthClient({ baseURL: import.meta.env.VITE_AUTH_URL ?? "http://localhost:3001", basePath: "/auth", + fetchPlugins: [displayNameMapper], }) export const { useSession, signIn, signUp, signOut } = authClient