Skip to content

Open source CapCut alternative.

TypeScript

feat(site): derive the Mac auth callback scheme automatically

David Le6d7643ffd295
diff --git a/docs/guides/backend-apis.md b/docs/guides/backend-apis.md
index 03a77963..44f6d888 100644
--- a/docs/guides/backend-apis.md
+++ b/docs/guides/backend-apis.md
@@ -56,8 +56,9 @@ through a periodic session check; the browser picks up an app sign-out when it
refetches the session on focus. That keeps the two in sync in both directions.
The hosted deploy needs `BETTER_AUTH_URL`, `BETTER_AUTH_SECRET`,
-`GOOGLE_CLIENT_ID`, `GOOGLE_CLIENT_SECRET`, and
-`DONKEY_MAC_AUTH_REDIRECT_ORIGINS`. Never commit real OAuth credentials.
+`GOOGLE_CLIENT_ID`, and `GOOGLE_CLIENT_SECRET`. The `donkey://` callback origin is
+derived automatically, so no env var configures it. Never commit real OAuth
+credentials.
## Handler Rules
diff --git a/site/.env.example b/site/.env.example
index 004db2cb..056a8354 100644
--- a/site/.env.example
+++ b/site/.env.example
@@ -5,8 +5,6 @@ BETTER_AUTH_URL="http://localhost:3000"
BETTER_AUTH_SECRET="replace-with-a-long-random-secret"
GOOGLE_CLIENT_ID="replace-with-google-web-client-id"
GOOGLE_CLIENT_SECRET="replace-with-google-web-client-secret"
-DONKEY_MAC_AUTH_REDIRECT_ORIGINS="donkey://"
-DONKEY_MAC_AUTH_CALLBACK_SCHEME="donkey"
# Required for macOS desktop computer-use through OpenAI.
OPENAI_API_KEY="replace-with-openai-api-key"
diff --git a/site/src/app/mac-auth/callback/route.ts b/site/src/app/mac-auth/callback/route.ts
index 186d6d7f..c01d5f0a 100644
--- a/site/src/app/mac-auth/callback/route.ts
+++ b/site/src/app/mac-auth/callback/route.ts
@@ -2,6 +2,7 @@ import { NextRequest, NextResponse } from "next/server";
import { z } from "zod";
import { auth } from "@/lib/auth";
+import { macAuthCallbackScheme } from "@/lib/mac-auth";
export const dynamic = "force-dynamic";
@@ -195,8 +196,7 @@ function macCallbackURL(
error?: string;
},
) {
- const callbackScheme = process.env.DONKEY_MAC_AUTH_CALLBACK_SCHEME ?? "donkey";
- const url = new URL(`${callbackScheme}://auth/callback`);
+ const url = new URL(`${macAuthCallbackScheme()}://auth/callback`);
url.searchParams.set("state", state);
if (query.code) {
diff --git a/site/src/lib/auth.ts b/site/src/lib/auth.ts
index 774831a6..8417f4ba 100644
--- a/site/src/lib/auth.ts
+++ b/site/src/lib/auth.ts
@@ -3,6 +3,7 @@ import { betterAuth } from "better-auth";
import { prismaAdapter } from "better-auth/adapters/prisma";
import { oneTimeToken } from "better-auth/plugins";
+import { macAuthRedirectOrigins } from "@/lib/mac-auth";
import { provisionSignupGrants } from "@/lib/onboarding/signup-grants";
import { prisma } from "@/lib/prisma";
@@ -10,17 +11,6 @@ import { prisma } from "@/lib/prisma";
// once at creation; only a hash is stored (handled by the apiKey plugin).
export const visionApiKeyPrefix = "dk_live_";
-function macAuthRedirectOrigins() {
- const configuredOrigins = process.env.DONKEY_MAC_AUTH_REDIRECT_ORIGINS
- ?.split(",")
- .map((origin) => origin.trim())
- .filter(Boolean);
-
- return configuredOrigins && configuredOrigins.length > 0
- ? configuredOrigins
- : ["donkey://"];
-}
-
// Both donkeyuse.com and www.donkeyuse.com serve the app, but the Google OAuth
// redirect_uri is pinned to BETTER_AUTH_URL's host. A sign-in started on one
// host would set a host-only state cookie there while the callback lands on the
diff --git a/site/src/lib/mac-auth.ts b/site/src/lib/mac-auth.ts
new file mode 100644
index 00000000..ee466e20
--- /dev/null
+++ b/site/src/lib/mac-auth.ts
@@ -0,0 +1,16 @@
+/**
+ * The custom URL scheme the website uses to hand a completed Mac sign-in back to
+ * the native app, plus the origin Better Auth trusts for that handoff.
+ *
+ * Local `next dev` targets the dev build's `donkey-dev://` (so the handoff never
+ * collides with an installed release that owns `donkey://`); every deployed build
+ * targets the shipped `donkey://`. Google OAuth is unaffected — its redirect_uri
+ * is the website's own callback, which never sees this scheme.
+ */
+export function macAuthCallbackScheme(): string {
+ return process.env.NODE_ENV === "development" ? "donkey-dev" : "donkey";
+}
+
+export function macAuthRedirectOrigins(): string[] {
+ return [`${macAuthCallbackScheme()}://`];
+}