Open source CapCut alternative.
TypeScript
Add Mac OAuth web handoff
David Lefdfcebc77676
diff --git a/docs/guides/backend-apis.md b/docs/guides/backend-apis.mdindex 9ec097d6..f8d4c452 100644--- a/docs/guides/backend-apis.md+++ b/docs/guides/backend-apis.md@@ -10,6 +10,12 @@ Backend APIs live in `site/src/app/api/**/route.ts`.- Better Auth is mounted as the public auth exception at `site/src/app/api/auth/[...all]/route.ts`; configure it through `@/lib/auth`.- The supported interactive login provider is Google OAuth only. Keep `emailAndPassword.enabled` false unless the product explicitly adds another login method.- Configure Google OAuth with `${BETTER_AUTH_URL}/api/auth/callback/google` as the redirect URI.+- Keep the Mac app callback origin (`donkey://` by default) in Better Auth+ trusted origins so `/mac-auth` can return successful Google sign-in to the+ app.+- In Vercel, configure `BETTER_AUTH_URL`, `BETTER_AUTH_SECRET`,+ `GOOGLE_CLIENT_ID`, `GOOGLE_CLIENT_SECRET`, and+ `DONKEY_MAC_AUTH_REDIRECT_ORIGINS`. Do not commit real OAuth credentials.- Validate request bodies, search params, and route params with Zod before using them.- Verify resource ownership before reading or mutating scoped data.- Return explicit `NextResponse.json(...)` responses with the intended status code.diff --git a/site/.env.example b/site/.env.exampleindex 6b849fc0..3a94007d 100644--- a/site/.env.example+++ b/site/.env.example@@ -3,5 +3,6 @@ DIRECT_URL="postgresql://postgres:[PASSWORD]@db.[PROJECT-REF].supabase.co:5432/pDONKEY_API_TOKEN="replace-with-a-long-random-token"BETTER_AUTH_URL="http://localhost:3000"BETTER_AUTH_SECRET="replace-with-a-long-random-secret"-GOOGLE_CLIENT_ID="replace-with-google-oauth-client-id"-GOOGLE_CLIENT_SECRET="replace-with-google-oauth-client-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://"diff --git a/site/src/app/mac-auth/MacAuthRedirector.tsx b/site/src/app/mac-auth/MacAuthRedirector.tsxnew file mode 100644index 00000000..2560b0c0--- /dev/null+++ b/site/src/app/mac-auth/MacAuthRedirector.tsx@@ -0,0 +1,168 @@+"use client";++import { ArrowRight, LoaderCircle } from "lucide-react";+import { useCallback, useEffect, useRef, useState } from "react";++import { authClient } from "@/lib/auth-client";++type Props = {+ callbackURL: string | null;+ errorCallbackURL: string | null;+};++type RedirectStatus = "opening" | "failed" | "missing-callback";++export function MacAuthRedirector({ callbackURL, errorCallbackURL }: Props) {+ const [status, setStatus] = useState<RedirectStatus>("opening");+ const hasStartedRef = useRef(false);++ const startGoogleAuth = useCallback(async () => {+ if (!callbackURL) {+ setStatus("missing-callback");+ return;+ }++ setStatus("opening");++ try {+ await authClient.signIn.social({+ callbackURL,+ errorCallbackURL: errorCallbackURL ?? callbackURL,+ provider: "google",+ });+ } catch {+ setStatus("failed");+ }+ }, [callbackURL, errorCallbackURL]);++ useEffect(() => {+ if (hasStartedRef.current) {+ return;+ }++ hasStartedRef.current = true;+ void startGoogleAuth();+ }, [startGoogleAuth]);++ const retry = useCallback(() => {+ hasStartedRef.current = true;+ void startGoogleAuth();+ }, [startGoogleAuth]);++ return (+ <main+ style={{+ alignItems: "center",+ background: "#1f201e",+ boxSizing: "border-box",+ color: "#f7f4ee",+ display: "flex",+ fontFamily: '-apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif',+ justifyContent: "center",+ minHeight: "100vh",+ padding: 24,+ }}+ >+ <section+ style={{+ alignItems: "center",+ display: "grid",+ gap: 18,+ justifyItems: "center",+ maxWidth: 460,+ textAlign: "center",+ width: "100%",+ }}+ >+ <div+ style={{+ alignItems: "center",+ background: "#df704a",+ borderRadius: 18,+ display: "flex",+ height: 68,+ justifyContent: "center",+ width: 68,+ }}+ >+ <LoaderCircle+ aria-hidden="true"+ size={34}+ strokeWidth={2.5}+ style={{+ animation: status === "opening" ? "donkey-spin 1s linear infinite" : undefined,+ }}+ />+ </div>++ <h1+ style={{+ fontSize: 42,+ fontWeight: 800,+ letterSpacing: 0,+ lineHeight: 1.05,+ margin: "22px 0 0",+ }}+ >+ {status === "opening" ? "Opening Google" : "Google sign-in paused"}+ </h1>++ <p+ style={{+ color: "rgba(247, 244, 238, 0.68)",+ fontSize: 17,+ lineHeight: 1.5,+ margin: 0,+ }}+ >+ {messageForStatus(status)}+ </p>++ {status === "failed" ? (+ <button+ onClick={retry}+ style={{+ alignItems: "center",+ background: "#f7f4ee",+ border: 0,+ borderRadius: 12,+ color: "#222",+ cursor: "pointer",+ display: "inline-flex",+ fontSize: 16,+ fontWeight: 800,+ gap: 10,+ height: 48,+ justifyContent: "center",+ marginTop: 14,+ padding: "0 22px",+ }}+ type="button"+ >+ Try again+ <ArrowRight aria-hidden="true" size={18} />+ </button>+ ) : null}+ </section>++ <style jsx>{`+ @keyframes donkey-spin {+ to {+ transform: rotate(360deg);+ }+ }+ `}</style>+ </main>+ );+}++function messageForStatus(status: RedirectStatus) {+ switch (status) {+ case "failed":+ return "Google sign-in could not start in this browser.";+ case "missing-callback":+ return "The Mac app callback was missing from this sign-in request.";+ case "opening":+ return "Use your Google account, then Donkey will bring you back to the Mac app.";+ }+}diff --git a/site/src/app/mac-auth/page.tsx b/site/src/app/mac-auth/page.tsxnew file mode 100644index 00000000..dfeb5897--- /dev/null+++ b/site/src/app/mac-auth/page.tsx@@ -0,0 +1,33 @@+import type { Metadata } from "next";++import { MacAuthRedirector } from "@/app/mac-auth/MacAuthRedirector";++export const metadata: Metadata = {+ title: "Sign in to Donkey for Mac",+ description: "Continue Google sign-in for Donkey for Mac.",+};++type SearchParams = Record<string, string | string[] | undefined>;++type Props = {+ searchParams: Promise<SearchParams>;+};++export default async function Page({ searchParams }: Props) {+ const params = await searchParams;++ return (+ <MacAuthRedirector+ callbackURL={firstParam(params.callbackURL)}+ errorCallbackURL={firstParam(params.errorCallbackURL)}+ />+ );+}++function firstParam(value: string | string[] | undefined) {+ if (Array.isArray(value)) {+ return value[0] ?? null;+ }++ return value ?? null;+}diff --git a/site/src/lib/auth.ts b/site/src/lib/auth.tsindex b86d4d91..06fbf694 100644--- a/site/src/lib/auth.ts+++ b/site/src/lib/auth.ts@@ -3,9 +3,21 @@ import { prismaAdapter } from "better-auth/adapters/prisma";import { prisma } from "@/lib/prisma";+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://"];+}+export const auth = betterAuth({baseURL: process.env.BETTER_AUTH_URL,secret: process.env.BETTER_AUTH_SECRET,+ trustedOrigins: macAuthRedirectOrigins(),database: prismaAdapter(prisma, {provider: "postgresql",}),