Skip to content

⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.

fix(web): the sign-in page keeps its options through a sign-out (#694)

Pasqual Tronconec2cba0000ecd
diff --git a/apps/web/src/components/session-scope.test.tsx b/apps/web/src/components/session-scope.test.tsx
new file mode 100644
index 00000000..5eca7726
--- /dev/null
+++ b/apps/web/src/components/session-scope.test.tsx
@@ -0,0 +1,91 @@
+import assert from 'node:assert/strict';
+import { afterEach, beforeEach, describe, it } from 'node:test';
+import { act } from 'react';
+import type { Root } from 'react-dom/client';
+import { JSDOM } from 'jsdom';
+import { QueryClient, QueryClientProvider, useQuery } from '@tanstack/react-query';
+import { qk } from '@/services/queryKeys';
+import { clearAccountQueries } from './session-scope';
+
+const replacedGlobals = [
+ 'window',
+ 'document',
+ 'navigator',
+ 'HTMLElement',
+ 'IS_REACT_ACT_ENVIRONMENT',
+] as const;
+let dom: JSDOM;
+let root: Root;
+let originalGlobalDescriptors: Map<string, PropertyDescriptor | undefined>;
+
+beforeEach(async () => {
+ originalGlobalDescriptors = new Map(
+ replacedGlobals.map((name) => [name, Object.getOwnPropertyDescriptor(globalThis, name)]),
+ );
+ dom = new JSDOM('<!doctype html><div id="root"></div>', { url: 'https://example.test/login' });
+ Object.defineProperties(globalThis, {
+ window: { configurable: true, value: dom.window },
+ document: { configurable: true, value: dom.window.document },
+ navigator: { configurable: true, value: dom.window.navigator },
+ HTMLElement: { configurable: true, value: dom.window.HTMLElement },
+ IS_REACT_ACT_ENVIRONMENT: { configurable: true, value: true },
+ });
+ const { createRoot } = await import('react-dom/client');
+ const element = document.querySelector('#root');
+ assert.ok(element);
+ root = createRoot(element);
+});
+
+afterEach(() => {
+ act(() => root.unmount());
+ dom.window.close();
+ for (const [name, descriptor] of originalGlobalDescriptors) {
+ if (descriptor) Object.defineProperty(globalThis, name, descriptor);
+ else Reflect.deleteProperty(globalThis, name);
+ }
+});
+
+describe('clearAccountQueries', () => {
+ // After a sign-out, the sign-in screen is mounted and waiting on the instance's
+ // configuration when the user change clears the cache.
+ it('keeps the sign-in configuration a mounted screen is still waiting on', async () => {
+ const client = new QueryClient();
+ let answer!: (config: { emailPassword: boolean }) => void;
+ function SignIn() {
+ const { data } = useQuery({
+ queryKey: qk.authConfig,
+ queryFn: () => new Promise<{ emailPassword: boolean }>((resolve) => (answer = resolve)),
+ });
+ return <p>{data ? `emailPassword=${data.emailPassword}` : 'no configuration'}</p>;
+ }
+ await act(async () =>
+ root.render(
+ <QueryClientProvider client={client}>
+ <SignIn />
+ </QueryClientProvider>,
+ ),
+ );
+
+ await act(async () => clearAccountQueries(client));
+ // React Query notifies its observers on a timer, not in the same tick.
+ await act(async () => {
+ answer({ emailPassword: false });
+ await new Promise((resolve) => setTimeout(resolve, 20));
+ });
+
+ assert.equal(document.querySelector('p')?.textContent, 'emailPassword=false');
+ });
+
+ it("drops the previous account's data", () => {
+ const client = new QueryClient();
+ client.setQueryData(qk.authConfig, { emailPassword: false });
+ client.setQueryData(qk.teams, [{ id: 1 }]);
+ client.getMutationCache().build(client, { mutationKey: ['watch'] });
+
+ clearAccountQueries(client);
+
+ assert.deepEqual(client.getQueryData(qk.authConfig), { emailPassword: false });
+ assert.equal(client.getQueryData(qk.teams), undefined);
+ assert.equal(client.getMutationCache().getAll().length, 0);
+ });
+});
diff --git a/apps/web/src/components/session-scope.tsx b/apps/web/src/components/session-scope.tsx
index 3c2935bf..ddef74bb 100644
--- a/apps/web/src/components/session-scope.tsx
+++ b/apps/web/src/components/session-scope.tsx
@@ -1,9 +1,10 @@
'use client';
import { useEffect, useRef } from 'react';
-import { useQueryClient } from '@tanstack/react-query';
+import { useQueryClient, type QueryClient } from '@tanstack/react-query';
import { useSession } from '@/lib/auth-client';
import { markSignedIn } from '@/lib/api/core/client';
+import { qk } from '@/services/queryKeys';
// Everything cached in this tab belongs to one account. Signing in as somebody else
// without a full page load (sign out and back in, a magic link opened in the same
@@ -13,6 +14,14 @@ import { markSignedIn } from '@/lib/api/core/client';
//
// Clearing on a changed user id fixes that. The remembered "last project" needs no
// cleanup: it is stored on the account, so the new session reads its own value.
+//
+// The instance's sign-in configuration is not account data, and the sign-in screen
+// may still be waiting on it when the session ends, so it stays.
+export function clearAccountQueries(queryClient: QueryClient) {
+ queryClient.removeQueries({ predicate: (query) => query.queryKey[0] !== qk.authConfig[0] });
+ queryClient.getMutationCache().clear();
+}
+
export default function SessionScope() {
const queryClient = useQueryClient();
const { data: session, isPending } = useSession();
@@ -27,7 +36,7 @@ export default function SessionScope() {
previous.current = userId;
if (before === undefined || before === userId) return;
if (userId) markSignedIn();
- queryClient.clear();
+ clearAccountQueries(queryClient);
}, [userId, isPending, queryClient]);
return null;