Skip to content

Open source CapCut alternative.

TypeScript

feat(site): a session started on the apex reaches its subdomains

David Le5319973e4a5e
diff --git a/docs/guides/cut/README.md b/docs/guides/cut/README.md
index 8c530455..ac9cbf3a 100644
--- a/docs/guides/cut/README.md
+++ b/docs/guides/cut/README.md
@@ -4,7 +4,7 @@ Donkey Cut is a free video editor that runs in the browser; code and paths short
A project lives in one of three homes, chosen when it is created. A browser project keeps its files in the page's own origin-private storage, so a fully local project needs nothing installed. A project on a Mac talks to the Donkey Cut engine — a local server the Donkey Mac app ships and supervises, which uses local disk, the app's bundled ffmpeg, on-device speech, and the user's own Claude/Codex CLI logins; the engine's projects and library belong to the Mac user, shared by every Donkey account that signs in on that Mac. A cloud project stores its doc and media on Donkey's hosted servers. The user chooses between the two places they think in — Local and Cloud — and which store backs Local is the session's business: the Mac's engine once the app has connected, the browser's own storage otherwise.
-AI generation — images, video, and voiceovers — and the assistant's Gemini models spend credits: whatever the project's home, the page posts to Donkey's hosted inference routes with the user's session and credits. `donkeycut.com` owns sign-in directly — the auth pages and the Google callback serve on that host — so the session is a plain host-only cookie. Generated media lands back in the project like any other file; Gemini chat turns, including their editor tool calls, run entirely in the page.
+AI generation — images, video, and voiceovers — and the assistant's Gemini models spend credits: whatever the project's home, the page posts to Donkey's hosted inference routes with the user's session and credits. `donkeycut.com` owns sign-in directly — the auth pages and the Google callback serve on that host — and the session cookie is scoped to `donkeycut.com`, so it also reaches the site's own subdomains. Generated media lands back in the project like any other file; Gemini chat turns, including their editor tool calls, run entirely in the page.
**The engine rule:** the engine API surface (`/api/cut/*`) runs only on the user's Mac. On a hosted deploy every one of those routes answers 404 before any handler runs, so the unauthenticated routes are unreachable there and nothing can execute off-Mac — the engine has no path to Donkey's production models; the page reaches them only through Donkey's own authenticated inference APIs. Don't wrap the engine's routes in the Donkey auth helper, reach for Prisma from them, or bill them against credits; local-only is the design for everything the engine does.
@@ -96,7 +96,7 @@ Every client call goes through a backend seam (`site/src/cut/lib/backend/`): the
The library is the exception, because it is the user's one shelf across places: every residency keeps a library, and the Library lists them at once as one listing, newest first, with a badge saying which shelf each item is on. So a cloud project can pull a clip off this Mac, and a browser project can pull one out of the cloud. A shelf that lives in the page holds its media in the same origin-private storage its projects use, and a link imported onto it rides the cloud worker — a page can't fetch a video off a YouTube link — with the bytes coming back down and the staged cloud copy dropped after. The Mac's shelf takes that same route as a second try: sites refuse fetches on the strength of the tool making them, so a link the engine's own tools can't bring back is fetched by the worker and lands here anyway. Each shelf is remembered on its own, so the Library tab keeps listing the Mac's items with the app closed, read-only, and clicking one raises the same banner an unreachable local project does. The editor's library picker asks only for what it can reach, since an item it cannot copy into the project has no business in a picker. Each item's own shelf answers for it: previews, renames, and deletes go to the backend it came from, and folders belong to one shelf, so items file only into folders beside them. Adding to a project is the one crossing: on the project's own shelf the server copies the file without the bytes leaving it, and off it the browser carries them down and back up. Filing an item into a folder on another shelf crosses the same way — the bytes come down and go back up, a file with its cover and a template with its own media, and the copy left behind comes off.
-The phone feeds the cloud shelf. Clips recorded in the iOS app upload themselves to it, marked as camera footage; that marker gives the desktop its Camera Roll — a tab of phone clips kept out of the main Library grid, and a locked Camera Roll folder in the editor's library panel so the footage drops into any project. Inspiration saved on the phone lands in an Inspiration folder on the same shelf, links riding the same worker import every shelf uses. What the phone put on the shelf it also mirrors: deleting a clip from the Camera Roll or the Library tombstones it, and the phone deletes its own copy on the next pass, so the roll on the phone and the roll at the desk stay one set. Notes are edited on both sides: the phone and the desktop's Notes tab work the same set, the newer edit winning, deletes carrying across. Notes file into folders of their own, on the shelf's folder table: the desktop files them by dragging onto the same folder tiles the Library uses, the phone by a folder list it opens like any other, and a folder made on either side is on the other by the next pass. Every request from the app carries a client marker, the server remembers accounts it has seen it from, and only those accounts get the phone tabs — a desktop-only account's sidebar never changes. The app's own guide covers the phone side: [The iOS App](../ios-app.md).
+The phone feeds the cloud shelf. Clips recorded in the iOS app upload themselves to it, marked as camera footage; that marker gives the desktop its Camera Roll — a tab of phone clips kept out of the main Library grid, and a locked Camera Roll folder in the editor's library panel so the footage drops into any project. A recording arrives named after the clock it was shot on, which says nothing about which clip it is, so each one is titled from itself: the page reads the opening of the clip and one frame, a light model names it in a few words, and the shelf keeps that answer, so the work happens once and the card, the viewer and the assistant all read the same title. The tile shows the title with the date under it, and hands over the whole line when the tile is too narrow to hold it. The file's own name stays what a prompt's reference resolves against. Inspiration saved on the phone lands in an Inspiration folder on the same shelf, links riding the same worker import every shelf uses. What the phone put on the shelf it also mirrors: deleting a clip from the Camera Roll or the Library tombstones it, and the phone deletes its own copy on the next pass, so the roll on the phone and the roll at the desk stay one set. Notes are edited on both sides: the phone and the desktop's Notes tab work the same set, the newer edit winning, deletes carrying across. Notes file into folders of their own, on the shelf's folder table: the desktop files them by dragging onto the same folder tiles the Library uses, the phone by a folder list it opens like any other, and a folder made on either side is on the other by the next pass. Every request from the app carries a client marker, the server remembers accounts it has seen it from, and only those accounts get the phone tabs — a desktop-only account's sidebar never changes. The app's own guide covers the phone side: [The iOS App](../ios-app.md).
Some of what the Library holds is lent. Most items are copied when they are used — a clip's bytes become the project's own — but a font is pointed at: every title set in it names it, and the file stays on the shelf where every project can reach it. Lent items follow three rules. They are identified by their bytes, so the same file on two shelves is one entry in the menu and one id in a document. A new one lands on the shelf every surface can read, whenever it is small and the account has room. And a project moving to the cloud takes what it points at with it, so a title keeps its typeface when it starts rendering there — since the id is the bytes, not a word of the document changes. Fonts are the first of these; a .ttf, .otf, .woff or .woff2 dropped on the Library is filed in folders, deleted, and counted against storage like anything else, and every project offers it for titles and captions. A font download arrives as a zip, so the zip is droppable: the faces come out of it and the licence text stays behind, and a family of eighteen weights becomes a single entry. The file is checked by installing it through the renderer's font seam before any bytes move, so what lands is what a preview and an export can draw with, and the family name comes off the font's own metadata. That same moment draws the specimen the shelf keeps as the file's cover, so a font card shows the alphabet in its own face whether or not the page looking at it has that face installed. A new account starts with an empty Fonts folder to say the shelf takes them; it is an ordinary folder and can be deleted. A render job reaches the cloud shelf alone, which is what those rules are for. A font filed into a folder belongs to that folder's shelf like anything else, and a full account keeps its font on the shelf it was working from: it draws in the tab, is marked in the font menu as living on this device, and falls back to the base face in a render job. A title set in a font that is later deleted falls back too, so nothing in a document has to be rewritten.
diff --git a/site/src/app/api/auth/[...all]/route.ts b/site/src/app/api/auth/[...all]/route.ts
index 4657cf25..b920e299 100644
--- a/site/src/app/api/auth/[...all]/route.ts
+++ b/site/src/app/api/auth/[...all]/route.ts
@@ -1,8 +1,57 @@
import { toNextJsHandler } from "better-auth/next-js";
+import { AUTH_COOKIE_DOMAIN } from "@/cut/lib/hosts";
import { auth } from "@/lib/auth";
-// donkeycut.com owns auth directly, so its cookies are already host-valid — no
-// per-host cookie re-scoping is needed. better-auth's handler serves GET/POST
-// for the whole /api/auth/* surface (sign-in, callback, session, sign-out).
-export const { GET, POST } = toNextJsHandler(auth);
+// better-auth's handler serves GET/POST for the whole /api/auth/* surface
+// (sign-in, callback, session, sign-out).
+const handler = toNextJsHandler(auth);
+
+const DOMAIN_ATTRIBUTE = `domain=${AUTH_COOKIE_DOMAIN}`;
+
+/** The names this response scopes to the registrable host. */
+function scopedCookieNames(headers: Headers): Set<string> {
+ const names = new Set<string>();
+ for (const cookie of headers.getSetCookie()) {
+ const [pair, ...attributes] = cookie.split(";");
+ const name = pair?.split("=")[0]?.trim();
+ if (!name) continue;
+ const scoped = attributes.some(
+ (a) => a.trim().toLowerCase() === DOMAIN_ATTRIBUTE,
+ );
+ if (scoped) names.add(name);
+ }
+ return names;
+}
+
+// Auth cookies carry Domain=donkeycut.com so the session reaches the site's
+// subdomains. Browsers that signed in before that still hold a host-only
+// cookie under the same name and send both, and better-auth expires only the
+// one it set: a sign-out would clear the scoped cookie and leave the older one
+// standing, whose session row is still live, signing the visitor straight back
+// in. So every response that writes a scoped cookie clears its host-only twin
+// in the same response, which also settles which of the two a request carries.
+// Rebuilt rather than mutated because a redirect response can arrive frozen.
+function clearHostOnlyTwins(response: Response): Response {
+ const names = scopedCookieNames(response.headers);
+ if (names.size === 0) return response;
+
+ const headers = new Headers(response.headers);
+ for (const name of names) {
+ headers.append(
+ "Set-Cookie",
+ `${name}=; Path=/; Max-Age=0; HttpOnly; Secure; SameSite=Lax`,
+ );
+ }
+ return new Response(response.body, {
+ headers,
+ status: response.status,
+ statusText: response.statusText,
+ });
+}
+
+export const GET = async (request: Request) =>
+ clearHostOnlyTwins(await handler.GET(request));
+
+export const POST = async (request: Request) =>
+ clearHostOnlyTwins(await handler.POST(request));
diff --git a/site/src/lib/auth.ts b/site/src/lib/auth.ts
index ad66404e..1b6d353c 100644
--- a/site/src/lib/auth.ts
+++ b/site/src/lib/auth.ts
@@ -2,18 +2,32 @@ import { betterAuth } from "better-auth";
import { prismaAdapter } from "better-auth/adapters/prisma";
import { bearer } from "better-auth/plugins";
-import { DONKEYCUT_CANONICAL } from "@/cut/lib/hosts";
+import { AUTH_COOKIE_DOMAIN, DONKEYCUT_CANONICAL, SU_ORIGIN } from "@/cut/lib/hosts";
import { provisionSignupGrants } from "@/lib/onboarding/signup-grants";
import { prisma } from "@/lib/prisma";
-// donkeycut.com is the single production host: the sign-in pages, the auth
-// API, the Google OAuth callback, and the session all live on that one origin
-// (the proxy 308s www. to the apex before anything serves), so auth cookies
-// stay plain host-only cookies. Hosted deploys pin baseURL there — it decides
-// the OAuth redirect_uri — while local dev leaves it unset and better-auth
-// derives it from the localhost request.
+// donkeycut.com owns sign-in: the auth pages, the auth API, and the Google
+// OAuth callback all serve on that one origin (the proxy 308s www. to the apex
+// before anything serves). Hosted deploys pin baseURL there — it decides the
+// OAuth redirect_uri — while local dev leaves it unset and better-auth derives
+// it from the localhost request.
const baseURL = process.env.VERCEL ? DONKEYCUT_CANONICAL : undefined;
+// The session is shared with the apex's subdomains: the super-user surface is
+// its own host, and a host-only cookie would never reach it. Scoping the auth
+// cookies to the registrable host lets the session ride across, and listing the
+// subdomain as a trusted origin lets a sign-in started there name its own
+// address as the post-auth callback. Hosted only — localhost can't carry a
+// Domain attribute, and dev serves both surfaces from one origin anyway.
+const crossSubDomain = process.env.VERCEL
+ ? {
+ advanced: {
+ crossSubDomainCookies: { domain: AUTH_COOKIE_DOMAIN, enabled: true },
+ },
+ trustedOrigins: [DONKEYCUT_CANONICAL, SU_ORIGIN],
+ }
+ : {};
+
// The iOS app signs in natively and exchanges the provider's ID token for a
// session at /api/auth/sign-in/social. ID tokens are verified against these
// native OAuth clients alongside the web client. The Google iOS client id is
@@ -23,6 +37,7 @@ const IOS_APP_BUNDLE_ID = "com.donkeycut.donkeycut";
const APPLE_SERVICES_ID = "com.donkeycut.signin";
export const auth = betterAuth({
+ ...crossSubDomain,
baseURL,
secret: process.env.BETTER_AUTH_SECRET,
// Sessions last a year, and the rolling expiry is refreshed daily on use, so an active user