Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix(auth): strip only the token on session expiry, keep the user (SCR-132)

Ezra Elletteeae893763b93
diff --git a/apps/screenpipe-app-tauri/lib/auth-guard.test.tsx b/apps/screenpipe-app-tauri/lib/auth-guard.test.tsx
index c9b494a92..a09d4140b 100644
--- a/apps/screenpipe-app-tauri/lib/auth-guard.test.tsx
+++ b/apps/screenpipe-app-tauri/lib/auth-guard.test.tsx
@@ -41,6 +41,7 @@ import {
isScreenpipeApi,
isScreenpipeAuthApi,
shouldReverifyOnFocus,
+ stripSessionToken,
} from "./auth-guard";
const LOGGED_IN = { token: "tok-123", cloud_subscribed: false };
@@ -125,6 +126,35 @@ describe("isScreenpipeAuthApi", () => {
});
});
+describe("stripSessionToken", () => {
+ it("returns null for a missing user", () => {
+ expect(stripSessionToken(null)).toBeNull();
+ expect(stripSessionToken(undefined)).toBeNull();
+ });
+
+ it("removes the token but preserves the profile + entitlement evidence", () => {
+ const user = {
+ id: "u-1",
+ email: "[email protected]",
+ token: "tok-123",
+ app_entitled: true,
+ cloud_subscribed: true,
+ subscription_plan: "enterprise",
+ };
+ const stripped = stripSessionToken(user)!;
+ expect("token" in stripped).toBe(false);
+ expect(stripped).toMatchObject({
+ id: "u-1",
+ email: "[email protected]",
+ app_entitled: true,
+ cloud_subscribed: true,
+ subscription_plan: "enterprise",
+ });
+ // original object untouched
+ expect(user.token).toBe("tok-123");
+ });
+});
+
describe("shouldReverifyOnFocus", () => {
it("allows the first verify of the session (lastVerifyAt === 0)", () => {
expect(shouldReverifyOnFocus(1_000, 0, "visible", 30_000)).toBe(true);
@@ -224,14 +254,20 @@ describe("AuthGuard session-expiry handling", () => {
mocks.state.user = { ...LOGGED_IN };
});
- it("signs the user out when a focus re-verify returns 401", async () => {
+ it("strips only the token (keeps the user) when a focus re-verify returns 401", async () => {
mocks.loadUser.mockRejectedValueOnce(
new Error("failed to verify token: 401 Unauthorized")
);
renderGuard();
fireEvent(window, new Event("focus"));
- await waitFor(() => expect(mocks.updateSettings).toHaveBeenCalledWith({ user: null }));
+ await waitFor(() => expect(mocks.updateSettings).toHaveBeenCalled());
+ // SCR-132: the user object must survive so the entitlement gate's
+ // transient-loss cushion can hold — only the token is removed.
+ const arg = mocks.updateSettings.mock.calls[0][0];
+ expect(arg.user).not.toBeNull();
+ expect(arg.user.token).toBeUndefined();
+ expect(arg.user.cloud_subscribed).toBe(false); // profile fields preserved
expect(mocks.setCloudToken).toHaveBeenCalledWith(null);
expect(mocks.capture).toHaveBeenCalledWith(
"session_expired",
@@ -239,14 +275,17 @@ describe("AuthGuard session-expiry handling", () => {
);
});
- it("signs the user out when a focus re-verify returns 403", async () => {
+ it("strips only the token (keeps the user) when a focus re-verify returns 403", async () => {
mocks.loadUser.mockRejectedValueOnce(
new Error("failed to verify token: 403 Forbidden")
);
renderGuard();
fireEvent(window, new Event("focus"));
- await waitFor(() => expect(mocks.updateSettings).toHaveBeenCalledWith({ user: null }));
+ await waitFor(() => expect(mocks.updateSettings).toHaveBeenCalled());
+ const arg = mocks.updateSettings.mock.calls[0][0];
+ expect(arg.user).not.toBeNull();
+ expect(arg.user.token).toBeUndefined();
expect(mocks.setCloudToken).toHaveBeenCalledWith(null);
});
diff --git a/apps/screenpipe-app-tauri/lib/auth-guard.tsx b/apps/screenpipe-app-tauri/lib/auth-guard.tsx
index 01163b6a1..f9a58860e 100644
--- a/apps/screenpipe-app-tauri/lib/auth-guard.tsx
+++ b/apps/screenpipe-app-tauri/lib/auth-guard.tsx
@@ -52,9 +52,12 @@ function showSignedOutToast() {
if (now - lastToastTime < TOAST_COOLDOWN_MS) return;
lastToastTime = now;
+ // Don't claim "app paused": with the entitlement evidence preserved (see
+ // stripSessionToken) a previously-entitled account keeps recording while
+ // signed out — only never-entitled accounts get gated.
toast({
- title: "signed out — app paused",
- description: "sign in with an active plan to keep using screenpipe.",
+ title: "session expired",
+ description: "sign in again to keep your account connected.",
variant: "destructive",
duration: 30000,
action: (
@@ -65,6 +68,24 @@ function showSignedOutToast() {
});
}
+// On session expiry, strip ONLY the token instead of nulling the whole user.
+// The persisted profile + entitlement evidence must survive so the entitlement
+// gate's transient-loss cushion (`failOpenForTransientAccessLoss`, keyed on
+// `isTokenHydrationPending` + `hasPersistedEntitlementEvidence` in
+// app-entitlement.ts) can keep a previously-entitled account recording and
+// onboarding intact while the user re-authenticates. Nulling the whole user
+// here is what looped enterprise users through the gate + onboarding after an
+// auto-update (SCR-132). An explicit, user-initiated logout still sets
+// `user: null` elsewhere — that path also triggers the cross-window sign-out
+// invalidation in updateSettings, which this deliberately does not.
+export function stripSessionToken<T extends { token?: string | null }>(
+ user: T | null | undefined,
+): T | null {
+ if (!user) return null;
+ const { token: _token, ...rest } = user as T & { token?: string | null };
+ return rest as T;
+}
+
function cloudRequestHost(url: string): string | null {
try {
const base =
@@ -132,6 +153,8 @@ export function AuthGuard({ children }: { children: React.ReactNode }) {
const { settings, updateSettings, loadUser } = useSettings();
const tokenRef = useRef(settings.user?.token);
tokenRef.current = settings.user?.token;
+ const userRef = useRef(settings.user);
+ userRef.current = settings.user;
const handleSessionExpired = useCallback(
async (context?: { source?: string; status?: number | null }) => {
@@ -145,7 +168,7 @@ export function AuthGuard({ children }: { children: React.ReactNode }) {
source: context?.source ?? "verify_token",
status: context?.status ?? null,
});
- await updateSettings({ user: null as any });
+ await updateSettings({ user: stripSessionToken(userRef.current) as any });
try {
await commands.setCloudToken(null);
} catch {}
diff --git a/apps/screenpipe-app-tauri/lib/hooks/use-settings.tsx b/apps/screenpipe-app-tauri/lib/hooks/use-settings.tsx
index 4bd8df043..beff1fb15 100644
--- a/apps/screenpipe-app-tauri/lib/hooks/use-settings.tsx
+++ b/apps/screenpipe-app-tauri/lib/hooks/use-settings.tsx
@@ -13,7 +13,7 @@ import posthog from "posthog-js";
import { cacheAnalyticsId } from "@/lib/analytics-id";
import { User } from "../utils/tauri";
import { SettingsStore } from "../utils/tauri";
-import { installAuthInterceptor } from "../auth-guard";
+import { installAuthInterceptor, stripSessionToken } from "../auth-guard";
import { hasAppEntitlement, normalizeAppUser } from "@/lib/app-entitlement";
import { screenpipeWebUrl } from "@/lib/web-url";
import type { SourceCitation } from "@/lib/source-citations";
@@ -1188,7 +1188,16 @@ export const SettingsProvider: React.FC<{ children: React.ReactNode }> = ({ chil
installAuthInterceptor(
() => settingsRef.current.user?.token ?? undefined,
async () => {
- await updateSettings({ user: null as any });
+ // Strip only the token — keep the profile + entitlement evidence so
+ // the entitlement gate's transient-loss cushion can hold instead of
+ // resetting onboarding (SCR-132). Because the user stays non-null,
+ // the explicit-logout invalidation in updateSettings ("user" in
+ // updates && !updates.user) intentionally does NOT fire: an
+ // in-flight loadUser that still succeeds may legitimately restore
+ // the session after a transient 401.
+ await updateSettings({
+ user: stripSessionToken(settingsRef.current.user) as any,
+ });
// Mirror the sign-out into the sidecar so the pi-agent and
// cloud_proxy.rs stop sending the now-revoked token on the
// next pipe run.