Files
classroomio/apps/dashboard/src/lib/utils/services/auth/client.ts
T

50 lines
1.6 KiB
TypeScript
Raw Normal View History

import { createAuthClient } from 'better-auth/svelte';
import { env } from '$env/dynamic/public';
2026-03-09 11:18:22 +01:00
import { ssoClient } from '@better-auth/sso/client';
import { dev } from '$app/environment';
2026-05-20 16:33:11 +01:00
// Browser baseURL depends on deployment shape:
//
// - 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`).
//
// 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';
}
if (dev) {
2026-05-20 16:33:11 +01:00
return env.PUBLIC_SERVER_URL || `${window.location.origin}/api/auth`;
}
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();
export const authClient = createAuthClient({
baseURL,
fetchOptions: {
credentials: 'include' // Include cookies in requests
2026-03-09 11:18:22 +01:00
},
plugins: [
ssoClient({
domainVerification: {
enabled: true
}
})
]
});