blockcn

Auth tokens

Send a bearer token with uploads and collaboration.

Uploads call getHeaders on each request, so the token can refresh. Collaboration takes token once per connection. Mint both on your server. Do not put a long-lived secret in the client bundle. See Backend.

This page's code block is examples/recipes/auth.ts. pnpm --dir docs typecheck compiles that file and fails if this block drifts.

import { useCollaboration } from "@/components/editor/collaboration/useCollaboration";
import { createBlockcnEditor } from "@/components/editor/preset";
import { media } from "@/components/editor/mediaAddon";
import { createUploadFile } from "@/lib/mediaUpload";

function bearer(): Record<string, string> {
  return { Authorization: "Bearer replace-with-a-short-lived-token" };
}

export const kit = createBlockcnEditor({
  addons: [media({ uploadFile: createUploadFile({ endpoint: "/api/upload", getHeaders: bearer }) })],
});

export function useRoom(room: string, token: string) {
  return useCollaboration({
    url: "wss://collab.example.com/collaboration",
    room,
    token,
  });
}