TanStack Start
Run withBetterSupabase as TanStack Start request middleware, with the caller's repositories in every server function.
toTanStackStart(entries) from better-supabase/tanstack-start returns the
.server() callback of a TanStack Start middleware. Register it as request
middleware so every request, server functions included, resolves the caller
once and refreshed session cookies reach the response:
import {
createCsrfMiddleware,
createMiddleware,
createStart,
} from "@tanstack/react-start";
import { createServer, withBetterSupabase } from "better-supabase/server";
import { toTanStackStart } from "better-supabase/tanstack-start";
import { betterSupabase } from "./lib/supabase";
const bsb = createServer(betterSupabase);
export const supabase = createMiddleware().server(
toTanStackStart([
withBetterSupabase(bsb, { refresh: true, allow: ["user", "none"] }),
]),
);
const csrf = createCsrfMiddleware({
filter: (ctx) => ctx.handlerType === "serverFn",
});
export const startInstance = createStart(() => ({
requestMiddleware: [csrf, supabase],
}));TanStack Start protects server functions with its own CSRF middleware only
while the app has no src/start.ts. Defining createStart replaces that
default, and Start logs a warning on the first server function request, so
list createCsrfMiddleware yourself as above (verified against
@tanstack/start-server-core 1.169).
Every key withBetterSupabase contributes lands on
context. A server function that uses the middleware reads it there:
import { createServerFn } from "@tanstack/react-start";
import { supabase } from "../start";
export const listNotes = createServerFn()
.middleware([supabase])
.handler(({ context }) => context.db.notes.list().orThrow());Refresh and short circuits
refresh: true belongs on request middleware: there the bridge returns the
response through the entries, so the refreshed cookies are added once. On
server function middleware the context still flows, but the function's
result has no Response to carry cookies.
A guard refusal (a 401 or 403 Problem Details) is thrown as a Response,
which TanStack Start sends instead of running the route. Use allow on a
second middleware for the routes that need a signed-in user, and keep the
request middleware open (allow: ["user", "none"]).
The bridge makes the request body readable twice, so an entry that reads it and the server function both see it.
better-supabase init detects TanStack Start from @tanstack/react-start, sets gen.hooks to tanstack-start and writes lib/supabase/middleware.ts with the supabase and csrf middleware above, unless the file exists. Register both in createStart yourself.
Session snapshot
The helpers from createTanStackStart(betterSupabase) return snapshot from
bsb.require(context). bsb.contextFromSession(snapshot) builds another
context from it with no second verification, so two loaders of one request
verify the token once:
const { snapshot } = await bsb.require(context);
const { db } = await bsb.contextFromSession(snapshot);A snapshot holds the access token: never return it in a response. A copy
of it (a spread, structuredClone, a serialized loader result) gets the
invalid context, so pass plain values such as the user id onward instead.
Prefetches refresh the session too. Parallel requests with the same refresh
token share one call to Auth, so they never race on it.
Function middleware
bsb.guard(options) is the .server() callback of a function middleware. It
runs require with the same options, adds the caller to context and sends a
refusal as require does. The request middleware must run first, as it does
for every server function once it is registered in src/start.ts.
import { createMiddleware } from "@tanstack/react-start";
export const tenantOnly = createMiddleware({ type: "function" }).server(
bsb.guard({ requireTenant: true }),
);Last updated on