Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(desktop): preserve project during auth recovery (#88289)

Peter Kirkhama29536c8e3d5
diff --git a/products/desktop/packages/core/src/auth/auth.test.ts b/products/desktop/packages/core/src/auth/auth.test.ts
index 5d289af94e09..0100376bba57 100644
--- a/products/desktop/packages/core/src/auth/auth.test.ts
+++ b/products/desktop/packages/core/src/auth/auth.test.ts
@@ -1285,6 +1285,58 @@ describe("AuthService", () => {
});
describe("transient org fetch failures", () => {
+ it("does not replace the stored project with another org's first project during partial recovery", async () => {
+ seedStoredSession({ selectedProjectId: 84 });
+ oauthFlow.refreshToken.mockResolvedValue(
+ mockTokenResponse({ scopedOrgs: ["org-1", "org-2"] }),
+ );
+
+ vi.stubGlobal(
+ "fetch",
+ vi.fn(async (input: string | Request) => {
+ const url = typeof input === "string" ? input : input.url;
+
+ if (url.includes("/api/users/@me/")) {
+ return {
+ ok: true,
+ json: vi.fn().mockResolvedValue({
+ uuid: "user-1",
+ organization: { id: "org-1" },
+ }),
+ } as unknown as Response;
+ }
+
+ if (url.includes("/api/organizations/org-1/")) {
+ return {
+ ok: true,
+ json: vi.fn().mockResolvedValue({
+ name: "Org 1",
+ teams: [{ id: 42, name: "Project 42" }],
+ }),
+ } as unknown as Response;
+ }
+
+ if (url.includes("/api/organizations/org-2/")) {
+ return {
+ ok: false,
+ status: 503,
+ json: vi.fn().mockResolvedValue({}),
+ } as unknown as Response;
+ }
+
+ return {
+ ok: true,
+ json: vi.fn().mockResolvedValue({ allowed: true, reason: null }),
+ } as unknown as Response;
+ }) as unknown as typeof fetch,
+ );
+
+ await service.initialize();
+
+ expect(service.getState().currentProjectId).toBeNull();
+ expect(sessionPort.getCurrent()?.selectedProjectId).toBe(84);
+ });
+
it("retries the org fetch on a transient network failure and keeps the selected project", async () => {
seedStoredSession({ selectedProjectId: 84 });
oauthFlow.refreshToken.mockResolvedValue(mockTokenResponse());
diff --git a/products/desktop/packages/core/src/auth/auth.ts b/products/desktop/packages/core/src/auth/auth.ts
index 1fdd07ad5891..27c80acbb560 100644
--- a/products/desktop/packages/core/src/auth/auth.ts
+++ b/products/desktop/packages/core/src/auth/auth.ts
@@ -815,13 +815,21 @@ export class AuthService extends TypedEventEmitter<AuthServiceEvents> {
const lastPrefs = accountKey
? this.authPreference.get(accountKey, options.cloudRegion)
: null;
+ const preferredProjectId =
+ options.selectedProjectId ?? lastPrefs?.lastSelectedProjectId ?? null;
const selection = this.reconcileInitialSelection({
orgProjectsMap,
currentOrgId,
- preferredProjectId:
- options.selectedProjectId ?? lastPrefs?.lastSelectedProjectId ?? null,
+ preferredProjectId,
lastSelectedOrgId: lastPrefs?.lastSelectedOrgId ?? null,
});
+ if (
+ orgProjectsIncomplete &&
+ preferredProjectId !== null &&
+ !flattenProjectIds(orgProjectsMap).includes(preferredProjectId)
+ ) {
+ selection.currentProjectId = null;
+ }
const refreshToken =
tokenResponse.refresh_token ?? options.fallbackRefreshToken ?? null;