Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(auth): retain stored token on non-401 errors in initialize (#1169)

Bohan Jiangf029eb01b831
diff --git a/packages/core/auth/store.test.ts b/packages/core/auth/store.test.ts
new file mode 100644
index 0000000000..20a2516948
--- /dev/null
+++ b/packages/core/auth/store.test.ts
@@ -0,0 +1,93 @@
+import { describe, expect, it, vi } from "vitest";
+import type { ApiClient } from "../api/client";
+import { ApiError } from "../api/client";
+import type { StorageAdapter, User } from "../types";
+import { createAuthStore } from "./store";
+
+const fakeUser: User = {
+ id: "u1",
+ name: "Alice",
+ email: "[email protected]",
+ avatar_url: null,
+} as User;
+
+function makeStorage(initial: Record<string, string> = {}): StorageAdapter & {
+ snapshot: () => Record<string, string>;
+} {
+ const data = { ...initial };
+ return {
+ getItem: (k) => data[k] ?? null,
+ setItem: (k, v) => {
+ data[k] = v;
+ },
+ removeItem: (k) => {
+ delete data[k];
+ },
+ snapshot: () => ({ ...data }),
+ };
+}
+
+function makeApi(getMe: () => Promise<User>): ApiClient {
+ return {
+ setToken: vi.fn(),
+ getMe,
+ // Only the methods touched by store.initialize are needed. Cast to
+ // ApiClient for type compatibility — the store treats it opaquely.
+ } as unknown as ApiClient;
+}
+
+describe("authStore.initialize — token mode", () => {
+ it("keeps the stored token when getMe fails with a non-401 ApiError (e.g. 500)", async () => {
+ const storage = makeStorage({ multica_token: "t" });
+ const api = makeApi(() =>
+ Promise.reject(new ApiError("server error", 500, "Internal Server Error")),
+ );
+ const store = createAuthStore({ api, storage });
+
+ await store.getState().initialize();
+
+ expect(store.getState().user).toBeNull();
+ expect(store.getState().isLoading).toBe(false);
+ expect(storage.snapshot().multica_token).toBe("t");
+ });
+
+ it("keeps the stored token on a network failure (non-ApiError throw)", async () => {
+ const storage = makeStorage({ multica_token: "t" });
+ const api = makeApi(() => Promise.reject(new TypeError("fetch failed")));
+ const store = createAuthStore({ api, storage });
+
+ await store.getState().initialize();
+
+ expect(store.getState().user).toBeNull();
+ expect(storage.snapshot().multica_token).toBe("t");
+ });
+
+ it("on 401, leaves storage cleanup to ApiClient.onUnauthorized and resets state", async () => {
+ // Simulate the real path: ApiClient fires onUnauthorized on 401, which
+ // removes the token from storage. The store's catch block must not
+ // duplicate or short-circuit this — it should only reset in-memory
+ // auth state.
+ const storage = makeStorage({ multica_token: "t" });
+ const api = makeApi(() => {
+ storage.removeItem("multica_token"); // stand-in for onUnauthorized
+ return Promise.reject(new ApiError("unauthorized", 401, "Unauthorized"));
+ });
+ const store = createAuthStore({ api, storage });
+
+ await store.getState().initialize();
+
+ expect(store.getState().user).toBeNull();
+ expect(storage.snapshot().multica_token).toBeUndefined();
+ });
+
+ it("populates user when getMe succeeds", async () => {
+ const storage = makeStorage({ multica_token: "t" });
+ const api = makeApi(() => Promise.resolve(fakeUser));
+ const store = createAuthStore({ api, storage });
+
+ await store.getState().initialize();
+
+ expect(store.getState().user).toEqual(fakeUser);
+ expect(storage.snapshot().multica_token).toBe("t");
+ });
+});
diff --git a/packages/core/auth/store.ts b/packages/core/auth/store.ts
index 41240845a4..b0e744d1b3 100644
--- a/packages/core/auth/store.ts
+++ b/packages/core/auth/store.ts
@@ -1,6 +1,6 @@
import { create } from "zustand";
import type { User, StorageAdapter } from "../types";
-import type { ApiClient } from "../api/client";
+import { ApiError, type ApiClient } from "../api/client";
import { setCurrentWorkspace } from "../platform/workspace-storage";
export interface AuthStoreOptions {
@@ -57,10 +57,17 @@ export function createAuthStore(options: AuthStoreOptions) {
try {
const user = await api.getMe();
set({ user, isLoading: false });
- } catch {
- api.setToken(null);
- setCurrentWorkspace(null, null);
- storage.removeItem("multica_token");
+ } catch (err) {
+ // Only clear the stored token on a genuine auth failure (401). For
+ // transient errors — network blips, backend rolling restarts, 5xx,
+ // aborted fetches — keep the token so the next initialize() (next
+ // page load or focus-refresh) can retry. The 401 path's token
+ // cleanup is handled upstream by ApiClient.handleUnauthorized via
+ // the onUnauthorized callback; we only need to reset the in-memory
+ // user + workspace state here.
+ if (err instanceof ApiError && err.status === 401) {
+ setCurrentWorkspace(null, null);
+ }
set({ user: null, isLoading: false });
}
},