Files
CubeAdmin/lib/auth/client.ts
2026-03-08 17:01:36 +01:00

53 lines
1.7 KiB
TypeScript

"use client";
import { createAuthClient } from "better-auth/react";
import { organizationClient } from "better-auth/client/plugins";
import { magicLinkClient } from "better-auth/client/plugins";
import type { Auth } from "./index";
/**
* Better Auth client instance for use in React components and client-side
* code. Mirrors the plugins registered on the server-side `auth` instance.
*/
export const authClient = createAuthClient({
// No baseURL — uses window.location.origin automatically, which always
// produces same-origin requests and avoids CSP connect-src issues.
...(process.env.NEXT_PUBLIC_BETTER_AUTH_URL
? { baseURL: process.env.NEXT_PUBLIC_BETTER_AUTH_URL }
: {}),
plugins: [
// Enables organization.* methods (createOrganization, getActiveMember, etc.)
organizationClient(),
// Enables signIn.magicLink and magicLink.verify
magicLinkClient(),
],
});
// ---------------------------------------------------------------------------
// Convenience re-exports so consumers only need to import from this module
// ---------------------------------------------------------------------------
export const {
signIn,
signOut,
signUp,
useSession,
getSession,
} = authClient;
// ---------------------------------------------------------------------------
// Inferred client-side types
// ---------------------------------------------------------------------------
export type ClientSession = typeof authClient.$Infer.Session.session;
export type ClientUser = typeof authClient.$Infer.Session.user;
/**
* Infer server plugin types on the client side.
* Provides full type safety for plugin-specific methods without importing
* the server-only `auth` instance into a client bundle.
*/
export type { Auth };