Browser client
Repositories in the browser that follow the session.
import { createClient } from "better-supabase/client";
import { betterSupabase } from "./index";
export const bsb = createClient(betterSupabase, {
env: {
url: import.meta.env.VITE_SUPABASE_URL,
publishableKey: import.meta.env.VITE_SUPABASE_PUBLISHABLE_KEY,
},
});
const customers = await bsb.db.customers
.list({ select: ["id", "name"] })
.orThrow();bsb.dbcarries the current session's actor and claims, so extensions like created-by and memberships behave as they do on the server. It is rebuilt when the user changes.bsb.queriesholds TanStack Query options.bsb.authis a small store (current(),subscribe()) for UI state:loading,signed-out, orsigned-inwith the user and decoded claims. The claims are for display; RLS is what enforces access.bsb.supabaseis the supabase-js client, for auth flows, storage and anything else.
Block hooks
The block hooks, such as useComments, useReactions and useTagInput,
read each block's schema, function names and realtime topic from the
blocks option, and act in the tenant from the memberships option:
import { blockConfig } from "./blocks.config";
export const bsb = createClient(betterSupabase, {
env,
blocks: blockConfig,
organization: () => currentOrganizationId(),
});Without organization, db and the hooks use the session's organization
claim (config.auth.tenant.claim). Pass a function for an organization
the user picks in the UI; it runs on every call. While there is none, the
hooks wait instead of calling the database.
Storage
storage | Use it for |
|---|---|
cookies (default) | apps with a server (Next.js, SvelteKit, …): the session is shared via @supabase/ssr cookies |
local | SPAs without a server: the session lives in localStorage |
With cookies, cookies: { encode: "tokens-only" } keeps the user object
out of the cookie, and auth.userStorage sets where auth-js keeps it instead
(localStorage by default). Use the same encode as the server; see
Encoding.
With local, the cookies options are ignored and no cookie is written, so
a server (a loader, an API route or the proxy) never sees the session. Send
the access token as an Authorization: Bearer header to call your own API;
the server adapters read the bearer token before the cookie. Pick one mode
per app: switching moves the session, so users sign in again once.
For React Native and Expo, use
better-supabase/client/native with your own
supabase-js client. It has the same bsb.db, bsb.queries and bsb.auth, never
imports @supabase/ssr, stores the session in the device keychain with
secureStorage, and refreshes it only in the foreground with
autoRefreshOnForeground.
The publishable key and URL are validated like on the server: a secret key or a legacy JWT key is rejected before any request is made.
Ports
bsb.ports holds the platform services the block hooks use: localStorage,
the Cmd+K listener, a file picker, deep links and uploads to Supabase
Storage. ports in the options adds or replaces them, such as a push port
with webPushToken({ vapidPublicKey }). See
block controllers.
Edge Functions
functions<Contracts>(bsb.supabase) calls Edge Functions defined with
defineFunction, typed from their own code, and returns a Result:
import { functions } from "better-supabase/client";
import type { CreateCustomer } from "../supabase/functions/create-customer/handler.ts";
const fns = functions<{ "create-customer": CreateCustomer }>(bsb.supabase);
const customer = await fns
.invoke("create-customer", { name: "Acme", organizationId })
.orThrow();See Edge Functions for the function side and
the errors invoke returns.
Last updated on