Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"dev": "vite",
"build": "tsc --noEmit && vite build",
"typecheck": "tsc --noEmit",
"test": "node --test src/api/client.test.ts src/lib/utils.test.ts src/lib/apiErrorCodes.test.ts src/lib/setup.test.ts src/lib/testEmail.test.ts",
"test": "node --test src/api/client.test.ts src/lib/utils.test.ts src/lib/apiErrorCodes.test.ts src/lib/setup.test.ts src/lib/testEmail.test.ts && vitest run",
"preview": "vite preview"
},
"dependencies": {
Expand All @@ -32,9 +32,11 @@
"@types/react-dom": "^19.0.0",
"@vitejs/plugin-react": "^6.0.0",
"autoprefixer": "^10.4.20",
"jsdom": "^30.1.3",
"postcss": "^8.4.49",
"tailwindcss": "^4.0.0",
"typescript": "^6.0.0",
"vite": "^8.0.0"
"vite": "^8.0.0",
"vitest": "^5.0.5"
}
}
285 changes: 285 additions & 0 deletions apps/web/src/pages/auth/TwoFactorPage.dom.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,285 @@
/**
* ONE-579 regression test — the 2FA completion must not bounce back to /login.
*
* The bug lived entirely in the browser: `handleSuccess` called
* `navigate('/dashboard')` and then `clearPendingState()`. BrowserRouter commits
* route state inside `React.startTransition` (a non-urgent lane) while the
* pending-state reset is an urgent update, so the page re-rendered without a
* pendingToken while `location` was still /two-factor; its
* `<Navigate to="/login">` guard then replaced the fresh /dashboard entry ~1ms
* later. No request ever failed, which is why the `node --test` suite (pure
* modules, no DOM) could not see it.
*
* This suite renders the real TwoFactorPage inside a real MemoryRouter — which,
* like BrowserRouter, runs route updates through `React.startTransition`
* (react-router's `useTransitions` default) — with only the transport (`fetch`)
* and the WebAuthn ceremony stubbed.
*
* Coverage (all three assertions fail if the latch is removed — they end up on
* /login, which is the reported symptom):
* 1. the latch itself: losing the pendingToken after it was held must not
* render the /login guard;
* 2. end to end for both verification branches (TOTP / Passkey): the final
* location is /dashboard and /login is never rendered;
* 3. the guard still works: opening the page without ever holding a token
* redirects to /login.
*/
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { act, useRef } from 'react';
import { createRoot, type Root } from 'react-dom/client';
import { MemoryRouter, Route, Routes, useLocation } from 'react-router-dom';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { AuthProvider, useAuth } from '@/contexts/AuthContext';
import { I18nProvider } from '@/i18n';
import TwoFactorPage from './TwoFactorPage';

(
globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }
).IS_REACT_ACT_ENVIRONMENT = true;

// The WebAuthn ceremony needs a real authenticator; what is under test is the
// code that runs after `passkey/verify` answers, so its result is canned.
vi.mock('@simplewebauthn/browser', () => ({
startAuthentication: vi.fn(async () => ({
id: 'cred-1',
rawId: 'cred-1',
response: {},
type: 'public-key',
clientExtensionResults: {},
})),
}));

const USER = {
id: 'u1',
email: 'owner@example.com',
username: 'owner',
role: 'admin' as const,
is_active: true,
totp_enabled: true,
email_2fa_enabled: false,
passkey_enabled: true,
created_at: 0,
};

/** Every pathname the router rendered, in order. */
let pathLog: string[] = [];
let container: HTMLDivElement;
let root: Root;

/** AuthContext handles, seeded from inside the provider (see `Seeder`). */
let seedPendingState: ((token: string, methods: string[]) => void) | null =
null;
let clearPendingState: (() => void) | null = null;

function LocationProbe() {
pathLog.push(useLocation().pathname);
return null;
}

// The app mounts this page only after the login step put a pendingToken into
// AuthContext, so the harness does the same: it renders nothing until a token
// exists and only then mounts the routes (a token-less mount would legitimately
// redirect to /login and pollute the assertions). Once mounted it stays mounted,
// which is what lets the /login guard become visible at all.
function Harness({ mountImmediately = false }: { mountImmediately?: boolean }) {
const { pendingToken } = useAuth();
const mountedOnce = useRef(false);
if (mountImmediately || pendingToken) mountedOnce.current = true;
if (!mountedOnce.current) return null;

return (
<Routes>
<Route path="/two-factor" element={<TwoFactorPage />} />
<Route path="/login" element={<div>login page</div>} />
<Route path="/dashboard" element={<div>dashboard page</div>} />
</Routes>
);
}

// Reaches the pending-state API of the real AuthProvider from inside it, so the
// test can reproduce the handoff from LoginPage without re-running the password
// step.
function Seeder() {
const auth = useAuth();
seedPendingState = auth.setPendingState;
clearPendingState = auth.clearPendingState;
return null;
}

function json(body: unknown, status = 200): Response {
return new Response(JSON.stringify(body), {
status,
headers: { 'Content-Type': 'application/json' },
});
}

/**
* Minimal transport stub. The first paint has no refresh cookie (the real one
* answers 401); every call after a successful verify succeeds.
*/
function stubFetch(verifyPath: string) {
const fetchMock = vi.fn(async (input: RequestInfo | URL) => {
const url = String(input);
if (url.endsWith('/auth/refresh')) {
return json({ error: 'unauthorized' }, 401);
}
if (url.endsWith('/auth/me')) return json({ user: USER });
if (url.endsWith(verifyPath)) {
return json({ accessToken: 'access-token', user: USER });
}
if (url.endsWith('/auth/2fa/passkey/verify-options')) {
return json({ options: { challenge: 'c' }, challengeId: 'ch-1' });
}
return json({ error: 'not stubbed' }, 500);
});
vi.stubGlobal('fetch', fetchMock);
return fetchMock;
}

/** Flush microtasks, timers and the transition render that carries navigation. */
async function settle() {
await act(async () => {
await Promise.resolve();
});
await act(async () => {
await new Promise((resolve) => setTimeout(resolve, 25));
});
}

function setInputValue(input: HTMLInputElement, value: string) {
const setter = Object.getOwnPropertyDescriptor(
window.HTMLInputElement.prototype,
'value',
)?.set;
setter?.call(input, value);
input.dispatchEvent(new Event('input', { bubbles: true }));
}

async function mountTwoFactorPage({
methods = ['totp'],
seed = true,
mountImmediately = false,
}: {
methods?: string[];
seed?: boolean;
mountImmediately?: boolean;
} = {}) {
container = document.createElement('div');
document.body.append(container);
root = createRoot(container);

await act(async () => {
root.render(
<QueryClientProvider
client={
new QueryClient({ defaultOptions: { queries: { retry: false } } })
}
>
<I18nProvider>
<MemoryRouter initialEntries={['/two-factor']}>
<AuthProvider>
<Seeder />
<LocationProbe />
<Harness mountImmediately={mountImmediately} />
</AuthProvider>
</MemoryRouter>
</I18nProvider>
</QueryClientProvider>,
);
});
await settle();

if (!seed) return;

expect(
seedPendingState,
'AuthProvider should expose setPendingState',
).toBeTypeOf('function');
await act(async () => {
seedPendingState?.('pending-token', methods);
});
await settle();
}

async function submitTotpCode(code: string) {
const input = container.querySelector('#totp-code') as HTMLInputElement;
expect(input, 'the TOTP form should be rendered').not.toBeNull();
await act(async () => {
setInputValue(input, code);
});
const form = container.querySelector('form') as HTMLFormElement;
await act(async () => {
form.requestSubmit();
});
await settle();
}

async function clickPasskeyVerify() {
const button = container.querySelector('button') as HTMLButtonElement;
expect(button, 'the passkey button should be rendered').not.toBeNull();
await act(async () => {
button.dispatchEvent(new MouseEvent('click', { bubbles: true }));
});
await settle();
}

beforeEach(() => {
pathLog = [];
seedPendingState = null;
clearPendingState = null;
localStorage.clear();
});

afterEach(async () => {
await act(async () => {
root?.unmount();
});
container?.remove();
vi.unstubAllGlobals();
vi.clearAllMocks();
});

describe('TwoFactorPage — ONE-579 landing after a successful verification', () => {
it('does not render the /login guard once the pending token is gone', async () => {
stubFetch('/auth/2fa/totp/verify');
await mountTwoFactorPage({ methods: ['totp'] });

expect(container.querySelector('#totp-code')).not.toBeNull();

// This is the urgent update that raced the /dashboard transition: it lands
// while the router still says /two-factor. Without the latch the guard
// fires here and sends the (already signed-in) user to /login.
await act(async () => {
clearPendingState?.();
});
await settle();

expect(pathLog.at(-1)).toBe('/two-factor');
expect(pathLog).not.toContain('/login');
});

it('still redirects to /login when opened without ever holding a token', async () => {
stubFetch('/auth/2fa/totp/verify');
await mountTwoFactorPage({ seed: false, mountImmediately: true });

expect(pathLog.at(-1)).toBe('/login');
});

it('TOTP: lands on /dashboard and never renders /login', async () => {
stubFetch('/auth/2fa/totp/verify');
await mountTwoFactorPage({ methods: ['totp'] });
await submitTotpCode('123456');

expect(pathLog.at(-1)).toBe('/dashboard');
expect(pathLog).not.toContain('/login');
});

it('Passkey: lands on /dashboard and never renders /login', async () => {
stubFetch('/auth/2fa/passkey/verify');
await mountTwoFactorPage({ methods: ['passkey'] });
await clickPasskeyVerify();

expect(pathLog.at(-1)).toBe('/dashboard');
expect(pathLog).not.toContain('/login');
});
});
33 changes: 24 additions & 9 deletions apps/web/src/pages/auth/TwoFactorPage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { startAuthentication } from '@simplewebauthn/browser';
import { useState } from 'react';
import { useRef, useState } from 'react';
import { Navigate, useNavigate, useSearchParams } from 'react-router-dom';
import { toast } from 'sonner';
import type { LoginResult } from '@/api/client';
Expand Down Expand Up @@ -41,9 +41,22 @@ export default function TwoFactorPage() {
const [loading, setLoading] = useState(false);
const [otpSent, setOtpSent] = useState(false);

// ONE-579: remember the pending token this page arrived with. `navigate()` in
// handleSuccess goes through BrowserRouter, which commits route state inside
// React.startTransition (a non-urgent lane), while `clearPendingState()` is an
// urgent update. On the urgent re-render `location` is therefore still
// /two-factor while `pendingToken` is already null, so the guard below used to
// fire and its <Navigate to="/login"> replaced the fresh /dashboard entry ~1ms
// later — the user ended up signed in but parked on /login. Keeping the last
// held token means the guard only fires when this page never had one (direct
// visit / stale tab), never during the teardown after a successful verify.
const lastPendingToken = useRef<string | null>(null);
if (pendingToken) lastPendingToken.current = pendingToken;
const activePendingToken = pendingToken ?? lastPendingToken.current;

// FE-C3: Redirect declaratively — calling navigate() during render is a side
// effect React may run twice (StrictMode) or warn about.
if (!pendingToken)
if (!activePendingToken)
return (
<Navigate
to={next ? `/login?next=${encodeURIComponent(next)}` : '/login'}
Expand All @@ -59,8 +72,10 @@ export default function TwoFactorPage() {
setAccessToken(result.accessToken);
broadcastRefresh();
await refreshUser();
// Leave the page before clearing the pending state: clearing it first
// re-renders this page without a pendingToken and flashes /login.
// Leave the page before clearing the pending state; the order alone is not
// enough (see lastPendingToken above) — the retained token is what keeps the
// teardown render from bouncing back to /login while the navigation is still
// a pending transition.
navigate(next ?? '/dashboard', { replace: true });
clearPendingState();
};
Expand All @@ -69,7 +84,7 @@ export default function TwoFactorPage() {
e.preventDefault();
setLoading(true);
try {
const result = await authApi.totpVerify(pendingToken, code);
const result = await authApi.totpVerify(activePendingToken, code);
await handleSuccess(result);
} catch (err) {
toastApiError(err, t('toast.invalidCode'), t);
Expand All @@ -81,7 +96,7 @@ export default function TwoFactorPage() {
const handleEmailSend = async () => {
setLoading(true);
try {
await authApi.emailOtpSend(pendingToken);
await authApi.emailOtpSend(activePendingToken);
setOtpSent(true);
toast.success(t('toast.codeSent'));
} catch (err) {
Expand All @@ -95,7 +110,7 @@ export default function TwoFactorPage() {
e.preventDefault();
setLoading(true);
try {
const result = await authApi.emailOtpVerify(pendingToken, code);
const result = await authApi.emailOtpVerify(activePendingToken, code);
await handleSuccess(result);
} catch (err) {
toastApiError(err, t('toast.invalidCode'), t);
Expand All @@ -108,10 +123,10 @@ export default function TwoFactorPage() {
setLoading(true);
try {
const { options, challengeId } =
await authApi.passkeyVerifyOptions(pendingToken);
await authApi.passkeyVerifyOptions(activePendingToken);
const response = await startAuthentication({ optionsJSON: options });
const result = await authApi.passkeyVerify(
pendingToken,
activePendingToken,
response,
challengeId,
);
Expand Down
Loading
Loading