:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(auth): ask for 2FA methods on the 2FA step only (#97810)
posthog[bot]5b76cc1f4974
diff --git a/frontend/src/scenes/authentication/login-2fa/login2FALogic.test.ts b/frontend/src/scenes/authentication/login-2fa/login2FALogic.test.tsnew file mode 100644index 000000000000..d454d29a810f--- /dev/null+++ b/frontend/src/scenes/authentication/login-2fa/login2FALogic.test.ts@@ -0,0 +1,61 @@+import { router } from 'kea-router'+import { expectLogic } from 'kea-test-utils'++import { loginTelemetryLogic } from 'scenes/authentication/shared/loginTelemetryLogic'+import { urls } from 'scenes/urls'++import { useMocks } from '~/mocks/jest'+import { initKeaTests } from '~/test/init'++import { login2FALogic } from './login2FALogic'++describe('login2FALogic', () => {+ let methodsHandler: jest.Mock++ beforeEach(() => {+ methodsHandler = jest.fn(() => [200, { has_totp: false, has_passkeys: true }])+ useMocks({ get: { '/api/login/2fa/passkey/methods/': methodsHandler } })+ initKeaTests()+ })++ // Kea mounts this logic alongside the telemetry logic these pages use, so the guard is that a+ // mount alone asks nothing. The server rejects the question outside a pending 2FA session.+ it.each([+ ['the login page', urls.login()],+ ['the password reset page', urls.passwordReset()],+ ])('does not ask which 2FA methods exist on %s', async (_name, url) => {+ router.actions.push(url)+ loginTelemetryLogic().mount()++ await expectLogic(login2FALogic).toFinishAllListeners()++ expect(methodsHandler).not.toHaveBeenCalled()+ })++ // A refresh or a hard load lands on the route before this logic exists, so the request has to+ // follow the current URL and not only a navigation to it.+ it('asks when it mounts on the 2FA step, so a refresh still offers a passkey', async () => {+ router.actions.push(urls.login2FA())+ login2FALogic().mount()++ await expectLogic(login2FALogic).toFinishAllListeners()++ expect(methodsHandler).toHaveBeenCalledTimes(1)+ await expectLogic(login2FALogic).toMatchValues({ passkeysAvailable: true })+ })++ it('asks again on every arrival at the 2FA step, so a passkey stays offered', async () => {+ router.actions.push(urls.login())+ loginTelemetryLogic().mount()++ router.actions.push(urls.login2FA())+ await expectLogic(login2FALogic).toFinishAllListeners()+ expect(methodsHandler).toHaveBeenCalledTimes(1)+ await expectLogic(login2FALogic).toMatchValues({ passkeysAvailable: true })++ router.actions.push(urls.login())+ router.actions.push(urls.login2FA())+ await expectLogic(login2FALogic).toFinishAllListeners()+ expect(methodsHandler).toHaveBeenCalledTimes(2)+ })+})diff --git a/frontend/src/scenes/authentication/login-2fa/login2FALogic.ts b/frontend/src/scenes/authentication/login-2fa/login2FALogic.tsindex 49c52a99aba1..f7a61d83fb52 100644--- a/frontend/src/scenes/authentication/login-2fa/login2FALogic.ts+++ b/frontend/src/scenes/authentication/login-2fa/login2FALogic.ts@@ -1,14 +1,16 @@import { type PublicKeyCredentialRequestOptionsJSON, startAuthentication } from '@simplewebauthn/browser'-import { MakeLogicType, actions, afterMount, connect, kea, listeners, path, reducers } from 'kea'+import { MakeLogicType, actions, connect, kea, listeners, path, reducers } from 'kea'import { forms } from 'kea-forms'import type { DeepPartial, DeepPartialMap, FieldName, ValidationErrorType } from 'kea-forms'import { loaders } from 'kea-loaders'+import { urlToAction } from 'kea-router'import api, { ApiError } from 'lib/api'import { featureFlagLogic } from 'lib/logic/featureFlagLogic'import { redirectAfterLogin } from 'scenes/authentication/login/loginLogic'import { preflightLogic } from 'scenes/PreflightCheck/preflightLogic'import { getPasskeyErrorMessage, isWebAuthnCancellation } from 'scenes/settings/user/passkeys/utils'+import { urls } from 'scenes/urls'import type { FeatureFlagsSet } from '../../../lib/logic/featureFlagLogic'import type { PreflightStatus } from '../../../types'@@ -300,8 +302,11 @@ export const login2FALogic = kea<login2FALogicType>([redirectAfterLogin()},})),- afterMount(({ actions }) => {- // Check if user has passkeys when component mounts- actions.checkPasskeysAvailable()- }),+ // The server answers this only for a pending 2FA session. Kea mounts this logic alongside+ // anything that references its actions, so the route, not the mount, decides when to ask.+ urlToAction(({ actions }) => ({+ [urls.login2FA()]: () => {+ actions.checkPasskeysAvailable()+ },+ })),])