2025-11-24 01:00:50 +01:00
|
|
|
import { createAuthClient } from 'better-auth/svelte';
|
2026-05-20 00:49:39 +01:00
|
|
|
import { env } from '$env/dynamic/public';
|
2026-03-09 11:18:22 +01:00
|
|
|
import { ssoClient } from '@better-auth/sso/client';
|
2026-05-21 11:29:47 +01:00
|
|
|
import { dev } from '$app/environment';
|
2025-11-24 01:00:50 +01:00
|
|
|
|
2026-05-20 16:33:11 +01:00
|
|
|
// Browser baseURL depends on deployment shape:
|
|
|
|
|
//
|
2026-06-07 18:43:05 +01:00
|
|
|
// - Self-hosted: browser auth calls stay on the dashboard origin at
|
|
|
|
|
// `/api/auth/*`; hooks.server.ts forwards them to the API over
|
|
|
|
|
// PRIVATE_SERVER_URL.
|
2026-05-20 16:33:11 +01:00
|
|
|
//
|
|
|
|
|
// - Cloud (multi-tenant): same-origin via the Cloudflare Worker so
|
|
|
|
|
// auth cookies stay host-only on whichever tenant/BYOD domain the
|
|
|
|
|
// user is on. We encode `<origin>/proxy/api/auth` because Better
|
|
|
|
|
// Auth's client treats any baseURL with a path as the full auth
|
|
|
|
|
// root verbatim (won't auto-append `/api/auth`).
|
2026-05-20 07:03:18 +01:00
|
|
|
//
|
|
|
|
|
// SSR: `authClient` methods are never invoked server-side (the dashboard
|
|
|
|
|
// uses `authServerClient` for that), but Better Auth's constructor still
|
|
|
|
|
// validates baseURL with `new URL(...)`, so we hand it the public API
|
|
|
|
|
// URL as an absolute-URL placeholder.
|
2026-05-20 16:33:11 +01:00
|
|
|
function resolveBaseURL() {
|
|
|
|
|
if (typeof window === 'undefined') {
|
|
|
|
|
return env.PUBLIC_SERVER_URL || 'http://localhost:3002';
|
|
|
|
|
}
|
2026-06-07 18:43:05 +01:00
|
|
|
if (dev) {
|
2026-05-20 16:33:11 +01:00
|
|
|
return env.PUBLIC_SERVER_URL || `${window.location.origin}/api/auth`;
|
|
|
|
|
}
|
2026-06-07 18:43:05 +01:00
|
|
|
if (env.PUBLIC_IS_SELFHOSTED === 'true') {
|
|
|
|
|
return `${window.location.origin}/api/auth`;
|
|
|
|
|
}
|
2026-05-20 16:33:11 +01:00
|
|
|
return `${window.location.origin}/proxy/api/auth`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const baseURL = resolveBaseURL();
|
2026-05-20 00:49:39 +01:00
|
|
|
|
2025-11-24 01:00:50 +01:00
|
|
|
export const authClient = createAuthClient({
|
2026-05-20 00:49:39 +01:00
|
|
|
baseURL,
|
2025-11-24 01:00:50 +01:00
|
|
|
fetchOptions: {
|
|
|
|
|
credentials: 'include' // Include cookies in requests
|
2026-03-09 11:18:22 +01:00
|
|
|
},
|
|
|
|
|
plugins: [
|
|
|
|
|
ssoClient({
|
|
|
|
|
domainVerification: {
|
|
|
|
|
enabled: true
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
]
|
2025-11-24 01:00:50 +01:00
|
|
|
});
|