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';
|
2025-11-24 01:00:50 +01:00
|
|
|
|
2026-05-20 07:03:18 +01:00
|
|
|
// Browser: same-origin via the Cloudflare Worker, so auth cookies stay
|
|
|
|
|
// host-only on whichever domain the user is on (app.classroomio.com,
|
|
|
|
|
// <org>.classroomio.school, or a BYOD domain). The URL is
|
|
|
|
|
// `<origin>/proxy/api/auth`:
|
|
|
|
|
// - `/proxy` is the Worker's same-origin escape hatch — it strips
|
|
|
|
|
// that prefix before forwarding to the upstream API.
|
|
|
|
|
// - `/api/auth` is where Better Auth is mounted on the Hono API.
|
|
|
|
|
// Better Auth's client treats any baseURL with a path as the auth root
|
|
|
|
|
// verbatim (it won't auto-append `/api/auth`), so we encode the full
|
|
|
|
|
// downstream path here.
|
|
|
|
|
//
|
|
|
|
|
// 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 00:49:39 +01:00
|
|
|
const baseURL =
|
2026-05-20 07:03:18 +01:00
|
|
|
typeof window !== 'undefined'
|
|
|
|
|
? `${window.location.origin}/proxy/api/auth`
|
|
|
|
|
: env.PUBLIC_SERVER_URL || 'http://localhost:3002';
|
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
|
|
|
});
|