5588c1b5d8
Avoids ERR_CONNECTION_REFUSED in deployed environments where VITE_AUTH_URL is not set at build time. Empty-string fallback routes auth requests to same origin, which the HTTPRoute forwards to the auth service. cc @cpfarhood Co-Authored-By: Paperclip <noreply@paperclip.ing>
37 lines
1023 B
TypeScript
37 lines
1023 B
TypeScript
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 || "",
|
|
basePath: "/auth",
|
|
fetchPlugins: [displayNameMapper],
|
|
})
|
|
|
|
export const { useSession, signIn, signUp, signOut } = authClient
|