Skip to content

Open source CapCut alternative.

TypeScript

refactor(site): drop the Mac app sign-in handoff

David Le9a4f94c2a0ab
diff --git a/site/src/app/mac-auth/MacAuthRedirector.tsx b/site/src/app/mac-auth/MacAuthRedirector.tsx
deleted file mode 100644
index 026c88d3..00000000
--- a/site/src/app/mac-auth/MacAuthRedirector.tsx
+++ /dev/null
@@ -1,108 +0,0 @@
-"use client";
-
-import { ArrowRight } from "lucide-react";
-import Image from "next/image";
-import { useCallback, useEffect, useRef, useState } from "react";
-
-import { authClient } from "@/lib/auth-client";
-
-type Props = {
- state: string | null;
-};
-
-type RedirectStatus = "opening" | "failed" | "missing-state";
-
-export function MacAuthRedirector({ state }: Props) {
- const [status, setStatus] = useState<RedirectStatus>("opening");
- const hasStartedRef = useRef(false);
-
- const startGoogleAuth = useCallback(async () => {
- if (!state) {
- setStatus("missing-state");
- return;
- }
-
- setStatus("opening");
- const encodedState = encodeURIComponent(state);
- const callbackURL = `/mac-auth/callback?state=${encodedState}`;
-
- try {
- await authClient.signIn.social({
- callbackURL,
- errorCallbackURL: `${callbackURL}&error=oauth`,
- provider: "google",
- });
- } catch {
- setStatus("failed");
- }
- }, [state]);
-
- useEffect(() => {
- if (hasStartedRef.current) {
- return;
- }
-
- hasStartedRef.current = true;
- void startGoogleAuth();
- }, [startGoogleAuth]);
-
- const retry = useCallback(() => {
- hasStartedRef.current = true;
- void startGoogleAuth();
- }, [startGoogleAuth]);
-
- return (
- <main className="box-border flex min-h-screen items-center justify-center bg-[#1f201e] px-6 py-6 text-[#f7f4ee]">
- <section className="grid w-full max-w-[460px] justify-items-center gap-[18px] text-center">
- <Image
- alt=""
- className="size-[72px] rounded-[18px] shadow-[0_18px_44px_rgba(0,0,0,0.32)]"
- height={72}
- src="/donkey-app-icon.png"
- width={72}
- />
-
- <h1 className="mt-[22px] text-[42px] leading-[1.05] font-semibold tracking-normal">
- {headingForStatus(status)}
- </h1>
-
- <p className="m-0 max-w-[420px] text-[17px] leading-6 text-[#f7f4ee]/68">
- {messageForStatus(status)}
- </p>
-
- {status === "failed" ? (
- <button
- className="mt-[14px] inline-flex h-12 cursor-pointer items-center justify-center gap-2.5 rounded-xl border-0 bg-[#f7f4ee] px-[22px] text-base font-semibold text-[#222]"
- onClick={retry}
- type="button"
- >
- Try again
- <ArrowRight aria-hidden="true" size={18} />
- </button>
- ) : null}
- </section>
-
- </main>
- );
-}
-
-function headingForStatus(status: RedirectStatus) {
- switch (status) {
- case "failed":
- case "missing-state":
- return "Google sign-in paused";
- case "opening":
- return "Continuing with Google";
- }
-}
-
-function messageForStatus(status: RedirectStatus) {
- switch (status) {
- case "failed":
- return "Google sign-in could not start in this browser.";
- case "missing-state":
- return "The Mac app state token 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/callback/route.ts b/site/src/app/mac-auth/callback/route.ts
deleted file mode 100644
index c01d5f0a..00000000
--- a/site/src/app/mac-auth/callback/route.ts
+++ /dev/null
@@ -1,211 +0,0 @@
-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";
-
-const callbackSearchParamsSchema = z.object({
- error: z.string().optional(),
- state: z.string().min(1),
-});
-
-/**
- * GET /mac-auth/callback
- *
- * Browser landing point after Better Auth completes Google OAuth for the Mac app.
- * It mints a short-lived one-time token from the browser session, then renders
- * a small handoff page that deep-links that code back to the native app for
- * cookie-jar exchange.
- */
-export async function GET(request: NextRequest) {
- const parsedParams = callbackSearchParamsSchema.safeParse(
- Object.fromEntries(request.nextUrl.searchParams),
- );
- if (!parsedParams.success) {
- return NextResponse.json(
- {
- error: "Invalid callback",
- message: "Missing Mac app state token.",
- },
- { status: 400 },
- );
- }
-
- const { error, state } = parsedParams.data;
- if (error) {
- return macHandoffPage(macCallbackURL(state, { error }), {
- heading: "Returning to Donkey",
- message: "Google sign-in did not finish. Donkey will ask you to try again.",
- });
- }
-
- try {
- const token = await auth.api.generateOneTimeToken({
- headers: request.headers,
- });
-
- return macHandoffPage(macCallbackURL(state, { code: token.token }), {
- heading: "You're signed in",
- message: "Donkey is opening on your Mac. You can close this tab after the app appears.",
- });
- } catch {
- return macHandoffPage(macCallbackURL(state, { error: "session" }), {
- heading: "Returning to Donkey",
- message: "Donkey could not create a Mac session yet. The app will ask you to try again.",
- });
- }
-}
-
-function macHandoffPage(
- url: URL,
- copy: {
- heading: string;
- message: string;
- },
-) {
- const callbackURL = url.toString();
- const scriptURL = JSON.stringify(callbackURL)
- .replace(/</g, "\\u003c")
- .replace(/\u2028/g, "\\u2028")
- .replace(/\u2029/g, "\\u2029");
-
- return new NextResponse(`<!doctype html>
-<html lang="en">
-<head>
- <meta charset="utf-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1" />
- <title>${escapeHTML(copy.heading)} - Donkey</title>
- <style>
- :root {
- color-scheme: dark;
- font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
- }
-
- * {
- box-sizing: border-box;
- }
-
- body {
- align-items: center;
- background: #1f201e;
- color: #f7f4ee;
- display: flex;
- justify-content: center;
- margin: 0;
- min-height: 100vh;
- padding: 24px;
- }
-
- main {
- align-items: center;
- display: grid;
- gap: 18px;
- justify-items: center;
- max-width: 460px;
- text-align: center;
- width: 100%;
- }
-
- img {
- border-radius: 18px;
- box-shadow: 0 18px 44px rgba(0, 0, 0, 0.32);
- height: 72px;
- width: 72px;
- }
-
- h1 {
- font-size: clamp(36px, 7vw, 48px);
- font-weight: 600;
- letter-spacing: 0;
- line-height: 1.04;
- margin: 18px 0 0;
- }
-
- p {
- color: rgba(247, 244, 238, 0.68);
- font-size: 17px;
- line-height: 1.5;
- margin: 0;
- max-width: 390px;
- }
-
- a {
- align-items: center;
- border: 1px solid rgba(247, 244, 238, 0.26);
- border-radius: 999px;
- color: #f7f4ee;
- display: inline-flex;
- font-size: 15px;
- font-weight: 600;
- height: 44px;
- justify-content: center;
- margin-top: 12px;
- padding: 0 20px;
- text-decoration: none;
- }
-
- a:focus-visible {
- outline: 3px solid rgba(115, 162, 255, 0.72);
- outline-offset: 3px;
- }
- </style>
-</head>
-<body>
- <main>
- <img alt="" src="/donkey-app-icon.png" />
- <h1>${escapeHTML(copy.heading)}</h1>
- <p>${escapeHTML(copy.message)}</p>
- <a href="${escapeHTMLAttribute(callbackURL)}">Open Donkey</a>
- </main>
- <script>
- const callbackURL = ${scriptURL};
- window.setTimeout(() => {
- window.location.href = callbackURL;
- }, 250);
- </script>
-</body>
-</html>`, {
- status: 200,
- headers: {
- "Cache-Control": "no-store",
- "Content-Type": "text/html; charset=utf-8",
- "Referrer-Policy": "no-referrer",
- },
- });
-}
-
-function escapeHTML(value: string) {
- return value
- .replace(/&/g, "&amp;")
- .replace(/</g, "&lt;")
- .replace(/>/g, "&gt;");
-}
-
-function escapeHTMLAttribute(value: string) {
- return escapeHTML(value)
- .replace(/"/g, "&quot;")
- .replace(/'/g, "&#39;");
-}
-
-function macCallbackURL(
- state: string,
- query: {
- code?: string;
- error?: string;
- },
-) {
- const url = new URL(`${macAuthCallbackScheme()}://auth/callback`);
- url.searchParams.set("state", state);
-
- if (query.code) {
- url.searchParams.set("code", query.code);
- }
-
- if (query.error) {
- url.searchParams.set("error", query.error);
- }
-
- return url;
-}
diff --git a/site/src/app/mac-auth/page.tsx b/site/src/app/mac-auth/page.tsx
deleted file mode 100644
index e2b04ecb..00000000
--- a/site/src/app/mac-auth/page.tsx
+++ /dev/null
@@ -1,32 +0,0 @@
-import type { Metadata } from "next";
-
-import { MacAuthRedirector } from "@/app/mac-auth/MacAuthRedirector";
-
-export const metadata: Metadata = {
- title: "Log in to Donkey for Mac",
- description: "Continue Google login 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
- state={firstParam(params.state)}
- />
- );
-}
-
-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.ts
index c47e5774..caf0fe72 100644
--- a/site/src/lib/auth.ts
+++ b/site/src/lib/auth.ts
@@ -1,10 +1,8 @@
import { apiKey } from "@better-auth/api-key";
import { betterAuth } from "better-auth";
import { prismaAdapter } from "better-auth/adapters/prisma";
-import { oneTimeToken } from "better-auth/plugins";
import { DONKEYCUT_CANONICAL } from "@/cut/lib/hosts";
-import { macAuthRedirectOrigins } from "@/lib/mac-auth";
import { provisionSignupGrants } from "@/lib/onboarding/signup-grants";
import { prisma } from "@/lib/prisma";
@@ -23,12 +21,8 @@ const baseURL = process.env.VERCEL ? DONKEYCUT_CANONICAL : undefined;
export const auth = betterAuth({
baseURL,
secret: process.env.BETTER_AUTH_SECRET,
- // The Mac-app handoff redirects to a custom URL scheme, which better-auth's
- // callback-origin check must trust.
- trustedOrigins: macAuthRedirectOrigins(),
- // Sessions last a year, and the rolling expiry is refreshed daily on use, so an active user effectively
- // never has to sign in again. The Mac app's native session cookie rides this same lifetime, keeping the
- // desktop signed in long after the handoff.
+ // Sessions last a year, and the rolling expiry is refreshed daily on use, so an active user
+ // effectively never has to sign in again.
session: {
expiresIn: 60 * 60 * 24 * 365,
updateAge: 60 * 60 * 24,
@@ -61,10 +55,6 @@ export const auth = betterAuth({
},
},
plugins: [
- oneTimeToken({
- expiresIn: 60,
- storeToken: "hashed",
- }),
apiKey({
// We enforce our own monthly call quota and per-key rate limit on the
// vision route, so the plugin's built-in rate limiting stays off.
diff --git a/site/src/lib/mac-auth.ts b/site/src/lib/mac-auth.ts
deleted file mode 100644
index ee466e20..00000000
--- a/site/src/lib/mac-auth.ts
+++ /dev/null
@@ -1,16 +0,0 @@
-/**
- * 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()}://`];
-}
diff --git a/site/src/proxy.ts b/site/src/proxy.ts
index fd20820c..3ed065e0 100644
--- a/site/src/proxy.ts
+++ b/site/src/proxy.ts
@@ -14,7 +14,7 @@ import {
//
// donkeycut.com "/" → landing, "/app/…" → editor app (generic
// "/…" → "/cut/…" rewrite). The auth pages (/sign-in,
-// /sign-up, /mac-auth), "/install", and the legal pages
+// /sign-up), "/install", and the legal pages
// pass through so the shared apex routes serve them
// same-host. www. 308s to the apex.
//
@@ -75,7 +75,6 @@ const DONKEYCUT_PASSTHROUGH = [
"/terms",
"/sign-in",
"/sign-up",
- "/mac-auth",
];
// Local dev mirrors donkeycut.com (Cut at "/", the editor at "/app/…"). It adds