Skip to content

Commit 37c3377

Browse files
authored
Merge pull request #97 from PI2-2026-2-equipe-03/fix/better-auth-baseurl
F5-26: cliente better-auth aponta para a origem do SPA e quebra login/cadastro em produção
2 parents 195a6db + e787467 commit 37c3377

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

‎src/lib/auth/client.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
import { createAuthClient } from 'better-auth/react'
22

3-
// same-origin: o browser roda em http://localhost:5173 e o vite proxia /api/auth/*
4-
// para o backend. em produção, basta expor /api/auth/* no mesmo host do SPA.
5-
export const authClient = createAuthClient()
3+
import { env } from '@/env'
4+
5+
// better-auth appends its own basePath (/api/auth) to this, so it takes the
6+
// api origin, never a path. without it the client falls back to the page
7+
// origin, which on a separate deploy is the spa host and answers 404 to every
8+
// session call. in dev VITE_API_URL is unset and the vite proxy serves
9+
// /api/auth from the same origin, which is why the bug only shows in prod.
10+
const apiOrigin = env.VITE_API_URL
11+
12+
export const authClient = createAuthClient(
13+
apiOrigin && /^https?:\/\//i.test(apiOrigin) ? { baseURL: apiOrigin } : {},
14+
)
615

716
export const { signIn, signUp, signOut, useSession } = authClient

0 commit comments

Comments
 (0)