Skip to content

Commit 4e2535b

Browse files
committed
fix(web): simplify session gate reentry
1 parent b1126e9 commit 4e2535b

2 files changed

Lines changed: 11 additions & 72 deletions

File tree

packages/web/src/features/auth/session-gate.test.tsx

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { activationStatusAtom } from "../../atoms/activation";
66
import { authEnabledAtom } from "../../atoms/connection";
77
import { SessionGatePage } from "./session-gate";
88

9-
const originalFetch = globalThis.fetch;
109
const originalLocation = window.location;
1110

1211
describe("SessionGatePage", () => {
@@ -22,14 +21,13 @@ describe("SessionGatePage", () => {
2221
});
2322

2423
afterEach(() => {
25-
globalThis.fetch = originalFetch;
2624
Object.defineProperty(window, "location", {
2725
configurable: true,
2826
value: originalLocation,
2927
});
3028
});
3129

32-
it("shows a password form when auth is enabled", () => {
30+
it("shows the session gate action even when auth is enabled", () => {
3331
const store = createStore();
3432
store.set(authEnabledAtom, true);
3533
store.set(activationStatusAtom, "gated");
@@ -42,76 +40,42 @@ describe("SessionGatePage", () => {
4240
</Provider>
4341
);
4442

45-
expect(screen.getByLabelText("密码")).toBeInTheDocument();
43+
expect(screen.queryByLabelText("密码")).not.toBeInTheDocument();
44+
expect(screen.getByRole("button", { name: "重新进入" })).toBeInTheDocument();
4645
});
4746

48-
it("claims re-entry after successful login when auth is enabled", async () => {
49-
const requestReentry = vi.fn().mockResolvedValue(true);
50-
globalThis.fetch = vi.fn().mockResolvedValueOnce({
51-
ok: true,
52-
json: async () => ({ ok: true }),
53-
}) as unknown as typeof fetch;
54-
55-
const store = createStore();
56-
store.set(authEnabledAtom, true);
57-
store.set(activationStatusAtom, "gated");
58-
59-
render(
60-
<Provider store={store}>
61-
<MemoryRouter initialEntries={["/session-gate"]}>
62-
<SessionGatePage requestReentry={requestReentry} />
63-
</MemoryRouter>
64-
</Provider>
65-
);
66-
67-
const input = await screen.findByLabelText("密码");
68-
fireEvent.change(input, { target: { value: "sekrit" } });
69-
fireEvent.click(screen.getByRole("button", { name: "确认" }));
70-
71-
await waitFor(() => {
72-
expect(requestReentry).toHaveBeenCalledTimes(1);
73-
});
74-
});
75-
76-
it("shows an explicit re-enter action when auth is disabled", async () => {
77-
const requestReentry = vi.fn().mockResolvedValue(true);
47+
it("shows an explicit re-enter action when auth is disabled", () => {
7848
const store = createStore();
7949
store.set(authEnabledAtom, false);
8050
store.set(activationStatusAtom, "gated");
8151

8252
render(
8353
<Provider store={store}>
8454
<MemoryRouter>
85-
<SessionGatePage requestReentry={requestReentry} />
55+
<SessionGatePage />
8656
</MemoryRouter>
8757
</Provider>
8858
);
8959

90-
fireEvent.click(screen.getByRole("button", { name: "重新进入" }));
91-
92-
await waitFor(() => {
93-
expect(requestReentry).toHaveBeenCalledTimes(1);
94-
});
60+
expect(screen.getByRole("button", { name: "重新进入" })).toBeInTheDocument();
9561
});
9662

97-
it("reloads the app after successful re-entry when auth is disabled", async () => {
98-
const requestReentry = vi.fn().mockResolvedValue(true);
63+
it("returns to the app bootstrap entry when re-enter is clicked", async () => {
9964
const store = createStore();
10065
store.set(authEnabledAtom, false);
10166
store.set(activationStatusAtom, "gated");
10267

10368
render(
10469
<Provider store={store}>
10570
<MemoryRouter initialEntries={["/session-gate"]}>
106-
<SessionGatePage requestReentry={requestReentry} />
71+
<SessionGatePage />
10772
</MemoryRouter>
10873
</Provider>
10974
);
11075

11176
fireEvent.click(screen.getByRole("button", { name: "重新进入" }));
11277

11378
await waitFor(() => {
114-
expect(requestReentry).toHaveBeenCalledTimes(1);
11579
expect(window.location.replace).toHaveBeenCalledWith("/");
11680
});
11781
});

packages/web/src/features/auth/session-gate.tsx

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
import { useAtomValue } from "jotai";
2-
import { useState } from "react";
3-
import { authEnabledAtom } from "../../atoms/connection";
41
import { Button, EmptyState } from "../../components/ui";
5-
import { useActivation } from "../../hooks/use-activation";
62
import { useViewport } from "../../hooks/use-viewport";
73
import { useTranslation } from "../../lib/i18n";
8-
import { LoginPage } from "./index";
94

105
const gateEmptyStateStyle = {
116
minHeight: "auto",
@@ -15,28 +10,9 @@ const gateEmptyStateStyle = {
1510
textAlign: "left" as const,
1611
};
1712

18-
export function SessionGatePage({ requestReentry }: { requestReentry?: () => Promise<boolean> }) {
13+
export function SessionGatePage() {
1914
const t = useTranslation();
20-
const authEnabled = useAtomValue(authEnabledAtom);
21-
const { claim } = useActivation();
2215
const isMobile = useViewport() === "mobile";
23-
const [submitting, setSubmitting] = useState(false);
24-
25-
const handleReentry = async () => {
26-
setSubmitting(true);
27-
try {
28-
const ok = requestReentry ? await requestReentry() : await claim();
29-
if (ok) {
30-
window.location.replace("/");
31-
}
32-
} finally {
33-
setSubmitting(false);
34-
}
35-
};
36-
37-
if (authEnabled === true) {
38-
return <LoginPage onAuthenticated={handleReentry} />;
39-
}
4016

4117
return (
4218
<div
@@ -78,10 +54,9 @@ export function SessionGatePage({ requestReentry }: { requestReentry?: () => Pro
7854
variant="primary"
7955
size="lg"
8056
type="button"
81-
disabled={submitting}
82-
onClick={() => void handleReentry()}
57+
onClick={() => window.location.replace("/")}
8358
>
84-
{submitting ? t("auth.session_gate_reentering") : t("auth.session_gate_reenter")}
59+
{t("auth.session_gate_reenter")}
8560
</Button>
8661
</div>
8762
</div>

0 commit comments

Comments
 (0)