: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): stop login telemetry from mounting the other auth logics (#98287)
Yasen5d62a4b234df
diff --git a/frontend/src/scenes/authentication/login-2fa/login2FALogic.test.ts b/frontend/src/scenes/authentication/login-2fa/login2FALogic.test.tsindex d454d29a810f..e4099a9a348d 100644--- a/frontend/src/scenes/authentication/login-2fa/login2FALogic.test.ts+++ b/frontend/src/scenes/authentication/login-2fa/login2FALogic.test.ts@@ -1,7 +1,6 @@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'@@ -18,14 +17,15 @@ describe('login2FALogic', () => {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.+ // Kea mounts this logic alongside anything that references its actions, which can happen on any+ // page, so a mount alone must ask 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()+ login2FALogic().mount()await expectLogic(login2FALogic).toFinishAllListeners()@@ -46,7 +46,7 @@ describe('login2FALogic', () => {it('asks again on every arrival at the 2FA step, so a passkey stays offered', async () => {router.actions.push(urls.login())- loginTelemetryLogic().mount()+ login2FALogic().mount()router.actions.push(urls.login2FA())await expectLogic(login2FALogic).toFinishAllListeners()diff --git a/frontend/src/scenes/authentication/shared/loginTelemetryLogic.test.ts b/frontend/src/scenes/authentication/shared/loginTelemetryLogic.test.tsindex 19f9dc09c5d2..9ec636b5fa62 100644--- a/frontend/src/scenes/authentication/shared/loginTelemetryLogic.test.ts+++ b/frontend/src/scenes/authentication/shared/loginTelemetryLogic.test.ts@@ -5,7 +5,7 @@ import posthog from 'posthog-js'import { login2FALogic } from 'scenes/authentication/login-2fa/login2FALogic'import { loginLogic } from 'scenes/authentication/login/loginLogic'import { passwordResetLogic } from 'scenes/authentication/password-reset/passwordResetLogic'-import { loginTelemetryLogic } from 'scenes/authentication/shared/loginTelemetryLogic'+import { LISTENED_ACTIONS, loginTelemetryLogic } from 'scenes/authentication/shared/loginTelemetryLogic'import { useMocks } from '~/mocks/jest'import { initKeaTests } from '~/test/init'@@ -24,6 +24,17 @@ function captureCount(event: string): number {return (posthog.capture as jest.Mock).mock.calls.filter(([name]) => name === event).length}+// The real action type behind each literal in LISTENED_ACTIONS, read off the logic that dispatches+// it. The Record type makes the compiler demand one entry per literal, the typed getter stops+// compiling when an action is renamed, and the imports above fail when a logic's module moves.+const LISTENED_ACTION_SOURCES: Record<keyof typeof LISTENED_ACTIONS, () => string> = {+ submitLoginRequest: () => loginLogic.actionTypes.submitLoginRequest,+ loginError: () => loginLogic.actionTypes.setGeneralError,+ codeVerificationFailure: () => loginLogic.actionTypes.submitCodeVerificationFailure,+ twoFactorError: () => login2FALogic.actionTypes.setGeneralError,+ resetRequested: () => passwordResetLogic.actionTypes.submitRequestPasswordResetSuccess,+}+describe('loginTelemetryLogic', () => {let logic: ReturnType<typeof loginTelemetryLogic.build>let login: ReturnType<typeof loginLogic.build>@@ -150,6 +161,40 @@ describe('loginTelemetryLogic', () => {expect(captureCount('login failed')).toBe(0)})+ // The listeners are keyed on strings, so nothing else ties a literal to the logic that dispatches+ // the action. A literal that drifts from the real type stops its listener without any error.+ it.each(Object.keys(LISTENED_ACTIONS) as (keyof typeof LISTENED_ACTIONS)[])(+ 'listens for the real action type of %s',+ (key) => {+ const twoFA = login2FALogic()+ const reset = passwordResetLogic()+ twoFA.mount()+ reset.mount()++ expect(LISTENED_ACTIONS[key]).toBe(LISTENED_ACTION_SOURCES[key]())++ twoFA.unmount()+ reset.unmount()+ }+ )++ // Hearing another logic's actions must not hold that logic mounted. A held logic keeps its state+ // between visits, so a failed second factor would show its old error on the next attempt.+ it('mounts no other logic', () => {+ logic.unmount()+ login.unmount()+ logic = loginTelemetryLogic()+ logic.mount()++ expect(loginLogic.findMounted()).toBeNull()+ expect(login2FALogic.findMounted()).toBeNull()+ expect(passwordResetLogic.findMounted()).toBeNull()++ // afterEach unmounts both logics, so restore the one the test took down+ login = loginLogic()+ login.mount()+ })+// precheck_failed must distinguish "the precheck ran and failed" from "no current precheck// exists" — a pending or stale-email result would otherwise be misattributed to this attempt.it.each([diff --git a/frontend/src/scenes/authentication/shared/loginTelemetryLogic.ts b/frontend/src/scenes/authentication/shared/loginTelemetryLogic.tsindex 278b9f6c9084..c056d4ba3163 100644--- a/frontend/src/scenes/authentication/shared/loginTelemetryLogic.ts+++ b/frontend/src/scenes/authentication/shared/loginTelemetryLogic.ts@@ -1,9 +1,7 @@import { MakeLogicType, afterMount, connect, kea, listeners, path } from 'kea'import posthog from 'posthog-js'-import { login2FALogic } from 'scenes/authentication/login-2fa/login2FALogic'import { loginLogic } from 'scenes/authentication/login/loginLogic'-import { passwordResetLogic } from 'scenes/authentication/password-reset/passwordResetLogic'import { preflightLogic } from 'scenes/PreflightCheck/preflightLogic'import { PreflightStatus } from '~/types'@@ -21,6 +19,26 @@ export type LoginFailureStep = 'login' | 'passkey' | 'code' | '2fa'*/let reportedError: { code: string; detail: string } | null = null+/**+ * The actions this logic listens to, named by their kea action type strings.+ *+ * Reading another logic's `actionTypes` while kea builds this logic makes kea connect that logic, and+ * a connected logic stays mounted for as long as this one is. The auth pages mount this logic, so a+ * connection would hold `loginLogic`, `login2FALogic` and `passwordResetLogic` mounted on pages that+ * do not use them, and their state would carry over from one visit to the next. String keys let this+ * logic hear the actions without the connection. A kea action type is `<action name as words>+ * (<logic path>)`, so renaming an action or changing its logic's path changes the string.+ * `loginTelemetryLogic.test.ts` reads each type off the real logic and fails when a string no longer+ * matches, when an action is renamed, or when a logic's module moves.+ */+export const LISTENED_ACTIONS = {+ submitLoginRequest: 'submit login request (scenes.authentication.login.loginLogic)',+ loginError: 'set general error (scenes.authentication.login.loginLogic)',+ codeVerificationFailure: 'submit code verification failure (scenes.authentication.login.loginLogic)',+ twoFactorError: 'set general error (scenes.authentication.login-2fa.login2FALogic)',+ resetRequested: 'submit request password reset success (scenes.authentication.password-reset.passwordResetLogic)',+} as const+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.export interface loginTelemetryLogicValues {preflight: PreflightStatus | null // preflightLogic@@ -59,14 +77,14 @@ export const loginTelemetryLogic = kea<loginTelemetryLogicType>([}}),listeners(({ values }) => ({- [loginLogic.actionTypes.submitLoginRequest]: () => {+ [LISTENED_ACTIONS.submitLoginRequest]: () => {// Only the password form dispatches this. An SSO sign-in leaves the page, and a passkey// prompt opens on its own, so `method` says which failures this denominator pairs with.posthog.capture('login attempted', { method: 'password', region: values.preflight?.region })},// Every terminal error of the password, SSO and passkey steps passes through// `setGeneralError`, so a submit failure and a redirect failure are both counted once.- [loginLogic.actionTypes.setGeneralError]: (payload: { code: string; detail: string }) => {+ [LISTENED_ACTIONS.loginError]: (payload: { code: string; detail: string }) => {if (payload === reportedError) {return}@@ -76,13 +94,13 @@ export const loginTelemetryLogic = kea<loginTelemetryLogicType>([// kea-forms carries the rejected response here, so the real reason survives. The manual-error// action cannot serve instead: it holds the message rather than the code, and it fires again// with an empty object when the person edits the code.- [loginLogic.actionTypes.submitCodeVerificationFailure]: ({ error }: { error: Error }) => {+ [LISTENED_ACTIONS.codeVerificationFailure]: ({ error }: { error: Error }) => {captureFailure('code', (error as unknown as { code?: string })?.code || 'invalid_code', values.preflight)},- [login2FALogic.actionTypes.setGeneralError]: ({ code }: { code: string }) => {+ [LISTENED_ACTIONS.twoFactorError]: ({ code }: { code: string }) => {captureFailure('2fa', code, values.preflight)},- [passwordResetLogic.actionTypes.submitRequestPasswordResetSuccess]: () => {+ [LISTENED_ACTIONS.resetRequested]: () => {posthog.capture('password reset requested', { region: values.preflight?.region })},})),