Skip to content

Commit 10b6f65

Browse files
committed
fix(web): offline launch no longer deletes T3 Connect environments
Clerk can finish loading signed out while the device is offline. The managed relay provider treated that first observation as a sign-out and removed every saved relay environment. Only clean up after a signed-in account was observed in this session. Fixes #17917
1 parent 397a461 commit 10b6f65

2 files changed

Lines changed: 66 additions & 3 deletions

File tree

‎apps/web/src/cloud/managedAuth.test.ts‎

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
11
import { managedRelaySessionAtom, setManagedRelaySession } from "@t3tools/client-runtime/relay";
22
import * as Effect from "effect/Effect";
3+
import * as Exit from "effect/Exit";
4+
import { AsyncResult } from "effect/reactivity";
5+
import { act, createElement } from "react";
6+
import { create, type ReactTestRenderer } from "react-test-renderer";
37
import { afterEach, describe, expect, it, vi } from "vite-plus/test";
48

59
import { appAtomRegistry } from "../rpc/atomRegistry";
610
import {
711
activateManagedRelayAuthentication,
812
deactivateManagedRelayAuthentication,
13+
ManagedRelayAuthProvider,
914
} from "./managedAuth";
1015

16+
const testState = vi.hoisted(() => ({
17+
auth: { isLoaded: false, isSignedIn: false, userId: null as string | null },
18+
getToken: async () => "token",
19+
removeRelayEnvironments: vi.fn(),
20+
}));
21+
1122
vi.mock("@clerk/react", () => ({
12-
useAuth: vi.fn(),
23+
useAuth: () => ({ ...testState.auth, getToken: testState.getToken }),
24+
}));
25+
26+
vi.mock("../state/use-atom-command", () => ({
27+
useAtomCommand: () => testState.removeRelayEnvironments,
1328
}));
1429

1530
vi.mock("../lib/runtime", () => ({
1631
runtime: {
17-
runPromiseExit: vi.fn(),
32+
runPromiseExit: vi.fn(async () => Exit.void),
1833
},
1934
}));
2035

@@ -24,10 +39,32 @@ vi.mock("../connection/catalog", () => ({
2439
},
2540
}));
2641

42+
let renderer: ReactTestRenderer | null = null;
43+
2744
afterEach(() => {
45+
act(() => renderer?.unmount());
46+
renderer = null;
47+
testState.auth = { isLoaded: false, isSignedIn: false, userId: null };
48+
testState.removeRelayEnvironments.mockReset();
2849
deactivateManagedRelayAuthentication();
2950
});
3051

52+
async function renderAuth(auth: typeof testState.auth) {
53+
testState.auth = auth;
54+
testState.removeRelayEnvironments.mockResolvedValue(AsyncResult.success(undefined));
55+
const element = createElement(ManagedRelayAuthProvider, null, null);
56+
await act(async () => {
57+
if (renderer) {
58+
renderer.update(element);
59+
} else {
60+
renderer = create(element);
61+
}
62+
});
63+
}
64+
65+
const signedOut = { isLoaded: true, isSignedIn: false, userId: null };
66+
const signedInAs = (userId: string) => ({ isLoaded: true, isSignedIn: true, userId });
67+
3168
describe("managed relay authentication", () => {
3269
it("clears all token access synchronously before account cleanup can fail", async () => {
3370
activateManagedRelayAuthentication("account-1", async () => "account-1-token");
@@ -56,3 +93,27 @@ describe("managed relay authentication", () => {
5693
expect(appAtomRegistry.get(managedRelaySessionAtom)?.accountId).toBe("account-2");
5794
});
5895
});
96+
97+
describe("ManagedRelayAuthProvider account cleanup", () => {
98+
it("keeps relay environments when the first loaded state is signed out", async () => {
99+
await renderAuth({ isLoaded: false, isSignedIn: false, userId: null });
100+
await renderAuth(signedOut);
101+
102+
expect(testState.removeRelayEnvironments).not.toHaveBeenCalled();
103+
});
104+
105+
it("removes relay environments when a signed-in account signs out", async () => {
106+
await renderAuth(signedInAs("account-1"));
107+
await renderAuth(signedOut);
108+
109+
expect(testState.removeRelayEnvironments).toHaveBeenCalledTimes(1);
110+
});
111+
112+
it("removes relay environments when the account changes", async () => {
113+
await renderAuth(signedInAs("account-1"));
114+
await renderAuth(signedInAs("account-2"));
115+
116+
expect(testState.removeRelayEnvironments).toHaveBeenCalledTimes(1);
117+
expect(appAtomRegistry.get(managedRelaySessionAtom)?.accountId).toBe("account-2");
118+
});
119+
});

‎apps/web/src/cloud/managedAuth.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ export function ManagedRelayAuthProvider({ children }: { readonly children: Reac
7171

7272
if (!isSignedIn || !userId) {
7373
deactivateManagedRelayAuthentication();
74-
if (previousAccount !== null) {
74+
// Clerk can load signed out while offline, so only a signed-in account
75+
// observed in this session is cleaned up.
76+
if (previousAccount !== undefined && previousAccount !== null) {
7577
void queueAccountCleanup();
7678
}
7779
} else {

0 commit comments

Comments
 (0)