Skip to content

Open source CapCut alternative.

TypeScript

feat(site): the app remembers its account and paints before the session resolves

David Le2b81232b3125
diff --git a/site/src/cut/components/NavUser.tsx b/site/src/cut/components/NavUser.tsx
index f3172f32..de7112c2 100644
--- a/site/src/cut/components/NavUser.tsx
+++ b/site/src/cut/components/NavUser.tsx
@@ -20,6 +20,7 @@ import {
} from "@/components/ui/dropdown-menu";
import { Skeleton } from "@/components/ui/skeleton";
import { UserAvatar } from "@/cut/components/UserAvatar";
+import { forgetRememberedEngineUser } from "@/cut/lib/api";
import { connectToEngine } from "@/cut/lib/connect";
import { cutInstallHref } from "@/cut/lib/install";
import { openOnboarding } from "@/cut/lib/onboarding";
@@ -77,6 +78,7 @@ export function NavUser() {
try {
await authClient.revokeSessions();
} finally {
+ forgetRememberedEngineUser();
await authClient.signOut();
router.push("/");
}
diff --git a/site/src/cut/components/RequireSession.tsx b/site/src/cut/components/RequireSession.tsx
index 64384254..ec4ff75e 100644
--- a/site/src/cut/components/RequireSession.tsx
+++ b/site/src/cut/components/RequireSession.tsx
@@ -3,7 +3,11 @@
import { useEffect, type ReactNode } from "react";
import { authHrefFor } from "@/app/_components/landing/useAppEntryHref";
-import { setEngineUser } from "@/cut/lib/api";
+import {
+ forgetRememberedEngineUser,
+ rememberedEngineUser,
+ setEngineUser,
+} from "@/cut/lib/api";
import { useAppLoaded } from "@/lib/analytics";
import { authClient } from "@/lib/auth-client";
@@ -25,6 +29,17 @@ export function RequireSession({ children }: { children: ReactNode }) {
useAppLoaded("cut", session?.user);
+ // Paint first, verify behind: the account remembered from the last visit
+ // opens the gate immediately, so a reload shows cached shelves while the
+ // session request is still in flight. The resolved session overrides it —
+ // a different account re-binds, a signed-out visitor is redirected. Ordered
+ // before the live binding below so a session that resolved during hydration
+ // wins on the same commit.
+ useEffect(() => {
+ const remembered = rememberedEngineUser();
+ if (remembered) setEngineUser(remembered);
+ }, []);
+
// Bound in an effect so the render stays free of side effects; SessionGate
// subscribes to it, so the surfaces redraw the moment the id lands.
useEffect(() => {
@@ -33,6 +48,7 @@ export function RequireSession({ children }: { children: ReactNode }) {
useEffect(() => {
if (!signedOut) return;
+ forgetRememberedEngineUser();
const here = window.location.pathname + window.location.search;
window.location.replace(authHrefFor("/sign-in", here));
}, [signedOut]);
diff --git a/site/src/cut/lib/api.ts b/site/src/cut/lib/api.ts
index 06004da4..10b29ece 100644
--- a/site/src/cut/lib/api.ts
+++ b/site/src/cut/lib/api.ts
@@ -145,9 +145,38 @@ const listeners = new Set<() => void>();
export function setEngineUser(id: string) {
if (engineUser === id) return;
engineUser = id;
+ try {
+ localStorage.setItem(REMEMBERED_USER_KEY, id);
+ } catch {
+ // Private mode or headless: the next visit waits on the session as before.
+ }
for (const fn of listeners) fn();
}
+const REMEMBERED_USER_KEY = "cut:user";
+
+/** The account that was signed in here last time. RequireSession binds it on
+ * boot so the surfaces paint their cached snapshots while the live session
+ * request is still in flight; the resolved session then confirms or replaces
+ * it. Cookies decide identity on every server read, so a stale id costs a
+ * wrong-keyed cache miss and nothing else. */
+export function rememberedEngineUser(): string | null {
+ try {
+ return localStorage.getItem(REMEMBERED_USER_KEY);
+ } catch {
+ return null;
+ }
+}
+
+/** Called on sign-out, so the next visitor to this browser starts unbound. */
+export function forgetRememberedEngineUser(): void {
+ try {
+ localStorage.removeItem(REMEMBERED_USER_KEY);
+ } catch {
+ // Nothing stored where nothing can be read.
+ }
+}
+
/** The bound account, or null before the session resolves. Client-side caches
* scope their keys to it so two accounts sharing a browser can't read each
* other's snapshots. */