Skip to content

Open source CapCut alternative.

TypeScript

fix(site): handle page translation and auth errors

David Le2cd67ba71905
diff --git a/site/src/app/_components/NoTranslate.tsx b/site/src/app/_components/NoTranslate.tsx
new file mode 100644
index 00000000..479b0127
--- /dev/null
+++ b/site/src/app/_components/NoTranslate.tsx
@@ -0,0 +1,18 @@
+"use client";
+
+import { useEffect } from "react";
+
+// Mounted by the Cut app. Its layout marks the app subtree translate="no", but
+// dialogs, menus, popovers, tooltips and toasts portal into <body>, outside
+// that subtree, where a browser's translator still rewrites the text nodes
+// React owns; the next commit that moves one throws NotFoundError out of
+// insertBefore or removeChild. Marking <html> covers every portal while the
+// app is mounted, and the marketing pages stay translatable once it unmounts.
+export function NoTranslate() {
+ useEffect(() => {
+ const html = document.documentElement;
+ html.setAttribute("translate", "no");
+ return () => html.removeAttribute("translate");
+ }, []);
+ return null;
+}
diff --git a/site/src/app/_components/landing/AuthPage.tsx b/site/src/app/_components/landing/AuthPage.tsx
index 404056fc..43cd4c46 100644
--- a/site/src/app/_components/landing/AuthPage.tsx
+++ b/site/src/app/_components/landing/AuthPage.tsx
@@ -1,25 +1,40 @@
-import { cookies } from "next/headers";
+import { cookies, headers } from "next/headers";
+import { redirect } from "next/navigation";
import { Suspense } from "react";
import { AuthScreen } from "@/app/_components/landing/AuthScreen";
import { CutFooter } from "@/app/cut/_components/landing/CutFooter";
-import { SIGN_IN_RETURN_COOKIE, signInReturnPath } from "@/lib/signInReturn";
+import { auth } from "@/lib/auth";
+import { resumeAfterAuthError, SIGN_IN_RETURN_COOKIE, signInReturnPath } from "@/lib/signInReturn";
export type AuthSearchParams = Promise<{
method?: string | string[];
callbackURL?: string | string[];
+ error?: string | string[];
}>;
type Props = { mode: "sign-in" | "sign-up"; searchParams: AuthSearchParams };
async function Form({ mode, searchParams }: Props) {
- const { method, callbackURL } = await searchParams;
+ const { method, callbackURL, error } = await searchParams;
+ const authError = typeof error === "string" ? error : undefined;
// A sign-in URL without its callback (a new tab mid-authorization) still
// returns to the authorization that sent this browser here.
const remembered = (await cookies()).get(SIGN_IN_RETURN_COOKIE)?.value;
const returnPath = signInReturnPath(typeof callbackURL === "string" ? callbackURL : undefined, remembered);
+
+ // A failed callback from a visitor who already holds a session (a replayed
+ // redirect) continues on; anyone else sees the error and tries again.
+ if (authError) {
+ const session = (await auth.api.getSession({ headers: await headers() })) ? "signed-in" : "signed-out";
+ const resume = resumeAfterAuthError(authError, session, returnPath);
+ if (resume) {
+ redirect(resume);
+ }
+ }
+
return <AuthScreen mode={mode} method={method === "email" ? "email" : "google"}
- callbackURL={returnPath} footer={<CutFooter />} />;
+ callbackURL={returnPath} authError={authError} footer={<CutFooter />} />;
}
export function AuthPage(props: Props) {
diff --git a/site/src/app/_components/landing/AuthScreen.tsx b/site/src/app/_components/landing/AuthScreen.tsx
index 5f76a715..da3ad336 100644
--- a/site/src/app/_components/landing/AuthScreen.tsx
+++ b/site/src/app/_components/landing/AuthScreen.tsx
@@ -15,6 +15,8 @@ type Props = {
mode: AuthMode;
method?: "google" | "email";
callbackURL?: string;
+ // The `?error=` code a failed provider callback brought back here.
+ authError?: string;
// CutFooter reads the blog on the server, so the page hands it in.
footer: ReactNode;
};
@@ -56,9 +58,11 @@ const copy = {
const GOOGLE_BUTTON_HEIGHT = 56;
-export function AuthScreen({ mode, method = "google", callbackURL, footer }: Props) {
+export function AuthScreen({ mode, method = "google", callbackURL, authError, footer }: Props) {
const [isPending, setIsPending] = useState(false);
- const [statusMessage, setStatusMessage] = useState<string | null>(null);
+ const [statusMessage, setStatusMessage] = useState<string | null>(
+ authError ? "Sign-in didn't finish. Please try again." : null,
+ );
const screenCopy = copy[mode];
const otherMode: AuthMode = mode === "sign-in" ? "sign-up" : "sign-in";
const alternateParams = new URLSearchParams();
diff --git a/site/src/app/cut/app/layout.tsx b/site/src/app/cut/app/layout.tsx
index 2c13db7d..1f827bd0 100644
--- a/site/src/app/cut/app/layout.tsx
+++ b/site/src/app/cut/app/layout.tsx
@@ -1,6 +1,7 @@
import { Suspense, type ReactNode } from "react";
import { NoSessionReplay } from "@/app/_components/NoSessionReplay";
+import { NoTranslate } from "@/app/_components/NoTranslate";
import { ConnectGate } from "@/cut/components/ConnectGate";
import { ExportsDock } from "@/cut/components/ExportsDock";
import { CutOnboarding } from "@/cut/components/onboarding/CutOnboarding";
@@ -38,6 +39,7 @@ export default function CutAppLayout({ children }: { children: ReactNode }) {
className="app-surface min-h-screen bg-white font-system text-foreground antialiased"
>
<NoSessionReplay />
+ <NoTranslate />
{/* A route whose address carries data — the editor's project id — can't be
in the prerendered shell, and the framework resolves that above the
client shell below. The boundary is here so it has somewhere to land. */}
diff --git a/site/src/lib/auth.ts b/site/src/lib/auth.ts
index 5644824d..182fd4ea 100644
--- a/site/src/lib/auth.ts
+++ b/site/src/lib/auth.ts
@@ -8,6 +8,7 @@ import { provisionSignupGrants } from "@/lib/onboarding/signup-grants";
import { prisma } from "@/lib/prisma";
import { emailPasswordDisabledPaths, emailPasswordGuard, emailPasswordOptions } from "@/lib/email-password-auth";
import { sendVerificationEmail } from "@/lib/email/send-verification";
+import { AUTH_ERROR_PATH } from "@/lib/signInReturn";
// 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
@@ -84,6 +85,10 @@ export const auth = betterAuth({
autoSignInAfterVerification: false,
},
disabledPaths: emailPasswordDisabledPaths,
+ // A provider callback that fails comes back to sign-in, which says so and
+ // offers the button again; left unset, production sends it to the landing
+ // page with only an `?error=` code to show for it.
+ onAPIError: { errorURL: AUTH_ERROR_PATH },
hooks: {
before: emailPasswordGuard,
},
diff --git a/site/src/lib/signInReturn.test.ts b/site/src/lib/signInReturn.test.ts
index 850b6bfc..18388a81 100644
--- a/site/src/lib/signInReturn.test.ts
+++ b/site/src/lib/signInReturn.test.ts
@@ -1,5 +1,5 @@
import { expect, test } from "bun:test";
-import { signInReturnCookie, signInReturnPath } from "@/lib/signInReturn";
+import { AUTH_ERROR_PATH, resumeAfterAuthError, signInReturnCookie, signInReturnPath } from "@/lib/signInReturn";
const value = (cookie: string) => decodeURIComponent(cookie.split(";")[0].split("=")[1]);
@@ -19,3 +19,18 @@ test("only same-site paths come back from the cookie", () => {
test("clearing expires the cookie", () => {
expect(signInReturnCookie(null)).toContain("Max-Age=0");
});
+
+test("a failed provider callback lands on sign-in", () => {
+ expect(AUTH_ERROR_PATH).toBe("/sign-in");
+});
+
+test("a signed-in visitor replaying a spent callback goes on to the app", () => {
+ // Back from the app into Google's redirect replays a state already used.
+ expect(resumeAfterAuthError("please_restart_the_process", "signed-in", "/app/p/1")).toBe("/app/p/1");
+ expect(resumeAfterAuthError("please_restart_the_process", "signed-in", undefined)).toBe("/app");
+});
+
+test("a signed-out visitor stays on sign-in to try again", () => {
+ expect(resumeAfterAuthError("please_restart_the_process", "signed-out", "/app")).toBeNull();
+ expect(resumeAfterAuthError(undefined, "signed-in", "/app")).toBeNull();
+});
diff --git a/site/src/lib/signInReturn.ts b/site/src/lib/signInReturn.ts
index b69516bd..4521ae35 100644
--- a/site/src/lib/signInReturn.ts
+++ b/site/src/lib/signInReturn.ts
@@ -1,3 +1,5 @@
+import { CUT_APP_BASE } from "@/cut/lib/appBase";
+
// Where sign-in returns when the sign-in URL lost its callback, for example
// when someone opens the email form in a new tab mid-authorization.
export const SIGN_IN_RETURN_COOKIE = "__Host-donkey-sign-in-return";
@@ -18,3 +20,26 @@ export function signInReturnPath(callbackURL: string | undefined, remembered: st
// Only a path on this site: "/x", never "//host" or "/\host".
return remembered && /^\/(?![/\\])/.test(remembered) ? remembered : undefined;
}
+
+/** Where better-auth sends a provider callback that failed, with `?error=<code>`. */
+export const AUTH_ERROR_PATH = "/sign-in";
+
+/**
+ * Where a visitor who landed on sign-in with an auth error goes next, or null
+ * to stay and try again.
+ *
+ * Most failed callbacks are replays: Back from the app returns to Google's
+ * redirect, which sends a state the first sign-in already spent. That visitor
+ * holds a session, so they continue where sign-in was headed.
+ */
+export function resumeAfterAuthError(
+ error: string | undefined,
+ session: "signed-in" | "signed-out",
+ returnPath: string | undefined,
+): string | null {
+ if (!error || session === "signed-out") {
+ return null;
+ }
+
+ return returnPath ?? CUT_APP_BASE;
+}