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): hydrate cloud token on the cross-window settings broadcast (#3943) (#4162)

Louis Beaumont2caa2d678c95
diff --git a/apps/screenpipe-app-tauri/lib/hooks/use-settings.tsx b/apps/screenpipe-app-tauri/lib/hooks/use-settings.tsx
index 82d5bfee5..cd954cc5b 100644
--- a/apps/screenpipe-app-tauri/lib/hooks/use-settings.tsx
+++ b/apps/screenpipe-app-tauri/lib/hooks/use-settings.tsx
@@ -711,6 +711,29 @@ async function setSettingsStripped(store: Store, settings: Settings) {
await store.set("settings", toPersist);
}
+/**
+ * #3943: the cloud auth token no longer lives in store.bin (it's in the
+ * encrypted secret store). Neither `store.get("settings")` nor the cross-window
+ * `onKeyChange` broadcast carries it, so hydrate it back into the in-memory
+ * settings here. Every reader of `settings.user.token` (the account "logged in"
+ * indicator, the auth auto-refresh effect, the engine Bearer path) then keeps
+ * working unchanged across windows. Without this on the broadcast path, a login
+ * in one window ships a token-stripped user to every window (including the one
+ * that just logged in), so they all render "not logged in". Mutates in place and
+ * returns the same object for convenience.
+ */
+async function hydrateCloudToken(settings: Settings): Promise<Settings> {
+ if (settings.user && !settings.user.token) {
+ try {
+ const token = await commands.getCloudToken();
+ if (token) settings.user.token = token;
+ } catch (e) {
+ console.warn("failed to hydrate cloud token from secret store:", e);
+ }
+ }
+ return settings;
+}
+
// Store utilities similar to Cap's implementation
function createSettingsStore() {
const get = async (): Promise<Settings> => {
@@ -720,18 +743,8 @@ function createSettingsStore() {
return createDefaultSettingsObject();
}
- // #3943: the cloud auth token no longer persists in store.bin (it now
- // lives in the encrypted secret store). Hydrate it back into the
- // in-memory settings so every existing reader of settings.user.token
- // keeps working unchanged — only persistence moved.
- if (settings.user && !settings.user.token) {
- try {
- const token = await commands.getCloudToken();
- if (token) settings.user.token = token;
- } catch (e) {
- console.warn("failed to hydrate cloud token from secret store:", e);
- }
- }
+ // #3943: re-hydrate the cloud token that no longer persists in store.bin.
+ await hydrateCloudToken(settings);
// Migration: Ensure existing users have deviceId for free tier tracking
let needsUpdate = false;
@@ -977,8 +990,15 @@ function createSettingsStore() {
const listen = (callback: (settings: Settings) => void) => {
return getStore().then((store) => {
- return store.onKeyChange("settings", (newValue: Settings | null | undefined) => {
- callback(newValue || createDefaultSettingsObject());
+ // #3943: the broadcast value is token-stripped (see setSettingsStripped),
+ // so hydrate the cloud token before handing settings to React, mirroring
+ // get(). A monotonic seq drops a slow hydration that a newer broadcast
+ // (e.g. a logout fired right after a login) has already superseded.
+ let seq = 0;
+ return store.onKeyChange("settings", async (newValue: Settings | null | undefined) => {
+ const mySeq = ++seq;
+ const next = await hydrateCloudToken(newValue || createDefaultSettingsObject());
+ if (mySeq === seq) callback(next);
});
});
};