Skip to content

Commit 7464ba3

Browse files
committed
remove dead initializeOAuth code
1 parent 8402745 commit 7464ba3

2 files changed

Lines changed: 6 additions & 63 deletions

File tree

apps/code/src/renderer/features/auth/stores/authStore.test.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { beforeEach, describe, expect, it, vi } from "vitest";
22

33
const mockGetState = vi.hoisted(() => ({ query: vi.fn() }));
4-
const mockOnStateChangedSubscribe = vi.hoisted(() => vi.fn());
54
const mockGetValidAccessToken = vi.hoisted(() => ({ query: vi.fn() }));
65
const mockRefreshAccessToken = vi.hoisted(() => ({ mutate: vi.fn() }));
76
const mockLogin = vi.hoisted(() => ({ mutate: vi.fn() }));
@@ -15,7 +14,6 @@ vi.mock("@renderer/trpc/client", () => ({
1514
trpcClient: {
1615
auth: {
1716
getState: mockGetState,
18-
onStateChanged: { subscribe: mockOnStateChangedSubscribe },
1917
getValidAccessToken: mockGetValidAccessToken,
2018
refreshAccessToken: mockRefreshAccessToken,
2119
login: mockLogin,
@@ -127,8 +125,6 @@ describe("authStore", () => {
127125
hasCodeAccess: null,
128126
needsScopeReauth: false,
129127
});
130-
mockOnStateChangedSubscribe.mockReturnValue({ unsubscribe: vi.fn() });
131-
132128
useAuthStore.setState({
133129
cloudRegion: null,
134130
staleCloudRegion: null,
@@ -146,12 +142,11 @@ describe("authStore", () => {
146142
});
147143
});
148144

149-
it("initializes from main auth state", async () => {
145+
it("syncs from main auth state", async () => {
150146
mockGetState.query.mockResolvedValue(authenticatedState);
151147

152-
const result = await useAuthStore.getState().initializeOAuth();
148+
await useAuthStore.getState().checkCodeAccess();
153149

154-
expect(result).toBe(true);
155150
expect(useAuthStore.getState().isAuthenticated).toBe(true);
156151
expect(useAuthStore.getState().projectId).toBe(1);
157152
});
@@ -170,7 +165,7 @@ describe("authStore", () => {
170165
it("deduplicates expensive renderer auth sync for repeated auth-state events", async () => {
171166
mockGetState.query.mockResolvedValue(authenticatedState);
172167

173-
await useAuthStore.getState().initializeOAuth();
168+
await useAuthStore.getState().checkCodeAccess();
174169
await useAuthStore.getState().checkCodeAccess();
175170

176171
expect(mockGetCurrentUser).toHaveBeenCalledTimes(1);
@@ -190,7 +185,7 @@ describe("authStore", () => {
190185
needsScopeReauth: false,
191186
});
192187

193-
await useAuthStore.getState().initializeOAuth();
188+
await useAuthStore.getState().checkCodeAccess();
194189
await useAuthStore.getState().checkCodeAccess();
195190

196191
expect(resetUser).toHaveBeenCalledTimes(1);

apps/code/src/renderer/features/auth/stores/authStore.ts

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,13 @@ import { ANALYTICS_EVENTS } from "@shared/types/analytics";
66
import type { CloudRegion } from "@shared/types/regions";
77
import { getCloudUrlFromRegion } from "@shared/utils/urls";
88
import { useNavigationStore } from "@stores/navigationStore";
9-
import {
10-
identifyUser,
11-
isFeatureFlagEnabled,
12-
resetUser,
13-
track,
14-
} from "@utils/analytics";
9+
import { identifyUser, resetUser, track } from "@utils/analytics";
1510
import { logger } from "@utils/logger";
1611
import { queryClient } from "@utils/queryClient";
1712
import { create } from "zustand";
1813

1914
const log = logger.scope("auth-store");
2015

21-
let initializePromise: Promise<boolean> | null = null;
22-
let authStateSubscription: { unsubscribe: () => void } | null = null;
2316
let sessionResetCallback: (() => void) | null = null;
2417
let inFlightAuthSync: Promise<void> | null = null;
2518
let inFlightAuthSyncKey: string | null = null;
@@ -30,8 +23,6 @@ export function setSessionResetCallback(callback: () => void) {
3023
}
3124

3225
export function resetAuthStoreModuleStateForTest(): void {
33-
initializePromise = null;
34-
authStateSubscription = null;
3526
sessionResetCallback = null;
3627
inFlightAuthSync = null;
3728
inFlightAuthSyncKey = null;
@@ -56,9 +47,7 @@ interface AuthStoreState {
5647
redeemInviteCode: (code: string) => Promise<void>;
5748
loginWithOAuth: (region: CloudRegion) => Promise<void>;
5849
signupWithOAuth: (region: CloudRegion) => Promise<void>;
59-
initializeOAuth: () => Promise<boolean>;
6050
selectProject: (projectId: number) => Promise<void>;
61-
completeOnboarding: () => void;
6251
selectPlan: (plan: "free" | "pro") => void;
6352
selectOrg: (orgId: string) => void;
6453
logout: () => Promise<void>;
@@ -204,22 +193,7 @@ async function syncAuthState(): Promise<void> {
204193
await inFlightAuthSync;
205194
}
206195

207-
function ensureAuthSubscription(): void {
208-
if (authStateSubscription) {
209-
return;
210-
}
211-
212-
authStateSubscription = trpcClient.auth.onStateChanged.subscribe(undefined, {
213-
onData: () => {
214-
void syncAuthState();
215-
},
216-
onError: (error) => {
217-
log.error("Auth state subscription error", { error });
218-
},
219-
});
220-
}
221-
222-
export const useAuthStore = create<AuthStoreState>((set, get) => ({
196+
export const useAuthStore = create<AuthStoreState>((set, _get) => ({
223197
cloudRegion: null,
224198
staleCloudRegion: null,
225199

@@ -263,39 +237,13 @@ export const useAuthStore = create<AuthStoreState>((set, get) => ({
263237
});
264238
},
265239

266-
initializeOAuth: async () => {
267-
if (initializePromise) {
268-
return initializePromise;
269-
}
270-
271-
initializePromise = (async () => {
272-
ensureAuthSubscription();
273-
await syncAuthState();
274-
return get().isAuthenticated || get().needsScopeReauth;
275-
})().finally(() => {
276-
initializePromise = null;
277-
});
278-
279-
return initializePromise;
280-
},
281-
282240
selectProject: async (projectId: number) => {
283241
sessionResetCallback?.();
284242
await trpcClient.auth.selectProject.mutate({ projectId });
285243
await syncAuthState();
286244
useNavigationStore.getState().navigateToTaskInput();
287245
},
288246

289-
completeOnboarding: () => {
290-
set({ hasCompletedOnboarding: true });
291-
if (
292-
isFeatureFlagEnabled("posthog-code-billing") &&
293-
!useSeatStore.getState().seat
294-
) {
295-
useSeatStore.getState().provisionFreeSeat();
296-
}
297-
},
298-
299247
selectPlan: (plan: "free" | "pro") => {
300248
set({ selectedPlan: plan });
301249
},

0 commit comments

Comments
 (0)