Skip to content

: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.

fix(auth): clear the re-authentication gate before signalling success (#99552)

posthog[bot]bd45cecbe78e
diff --git a/frontend/src/lib/components/TimeSensitiveAuthentication/timeSensitiveAuthenticationLogic.test.ts b/frontend/src/lib/components/TimeSensitiveAuthentication/timeSensitiveAuthenticationLogic.test.ts
index 4aafc4952f41..7621e7154984 100644
--- a/frontend/src/lib/components/TimeSensitiveAuthentication/timeSensitiveAuthenticationLogic.test.ts
+++ b/frontend/src/lib/components/TimeSensitiveAuthentication/timeSensitiveAuthenticationLogic.test.ts
@@ -66,6 +66,21 @@ describe('timeSensitiveAuthenticationLogic', () => {
})
})
+ it('should clear the gate even when the sensitive action throws', async () => {
+ userLogic.actions.loadUserSuccess(MOCK_DEFAULT_USER)
+ const onSuccess = jest.fn(() => {
+ throw new Error('sensitive action failed')
+ })
+ apiStatusLogic.actions.setTimeSensitiveAuthenticationRequired([onSuccess, jest.fn()])
+
+ logic.actions.setReauthenticationValues({ password: 'test', token: undefined })
+ logic.actions.submitReauthentication()
+ await expectLogic(logic).toFinishAllListeners()
+
+ expect(onSuccess).toHaveBeenCalledTimes(1)
+ await expectLogic(logic).toMatchValues({ showAuthenticationModal: false })
+ })
+
it('should handle successful reauthentication', async () => {
userLogic.actions.loadUserSuccess(MOCK_DEFAULT_USER)
diff --git a/frontend/src/lib/components/TimeSensitiveAuthentication/timeSensitiveAuthenticationLogic.ts b/frontend/src/lib/components/TimeSensitiveAuthentication/timeSensitiveAuthenticationLogic.ts
index 1fe65445bc0c..317e104b04fd 100644
--- a/frontend/src/lib/components/TimeSensitiveAuthentication/timeSensitiveAuthenticationLogic.ts
+++ b/frontend/src/lib/components/TimeSensitiveAuthentication/timeSensitiveAuthenticationLogic.ts
@@ -67,6 +67,9 @@ export interface timeSensitiveAuthenticationLogicValues {
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface timeSensitiveAuthenticationLogicActions {
+ resolveSensitiveAction: (outcome: 'failure' | 'success') => {
+ outcome: 'failure' | 'success'
+ } // apiStatusLogic
setTimeSensitiveAuthenticationRequired: (required: boolean | [onSuccess: () => void, onFailure: () => void]) => {
required: boolean | [onSuccess: () => void, onFailure: () => void]
} // apiStatusLogic
@@ -211,7 +214,12 @@ export const timeSensitiveAuthenticationLogic = kea<timeSensitiveAuthenticationL
modalInterruptionTrackingLogic,
['interruptedForm'],
],
- actions: [apiStatusLogic, ['setTimeSensitiveAuthenticationRequired'], userLogic, ['loadUser']],
+ actions: [
+ apiStatusLogic,
+ ['setTimeSensitiveAuthenticationRequired', 'resolveSensitiveAction'],
+ userLogic,
+ ['loadUser'],
+ ],
logic: [modalInterruptionTrackingLogic],
})),
actions({
@@ -382,34 +390,24 @@ export const timeSensitiveAuthenticationLogic = kea<timeSensitiveAuthenticationL
listeners(({ actions, values }) => ({
submitReauthenticationSuccess: () => {
- if (Array.isArray(values.timeSensitiveAuthenticationRequired)) {
- values.timeSensitiveAuthenticationRequired[0]() // Signal success
- }
posthog.capture('reauthentication_completed')
- actions.setTimeSensitiveAuthenticationRequired(false)
+ actions.resolveSensitiveAction('success')
// Refresh the user so we know the new session expiry
actions.loadUser()
},
beginPasskey2FASuccess: () => {
- if (Array.isArray(values.timeSensitiveAuthenticationRequired)) {
- values.timeSensitiveAuthenticationRequired[0]() // Signal success
- }
posthog.capture('reauthentication_completed', { method: 'passkey_2fa' })
- actions.setTimeSensitiveAuthenticationRequired(false)
+ actions.resolveSensitiveAction('success')
// Refresh the user so we know the new session expiry
actions.loadUser()
},
submitReauthenticationFailure: () => {
- if (Array.isArray(values.timeSensitiveAuthenticationRequired)) {
- values.timeSensitiveAuthenticationRequired[1]() // Signal failure/dismissal
- }
+ actions.resolveSensitiveAction('failure')
},
setDismissedReauthentication: ({ value }) => {
if (value) {
- if (Array.isArray(values.timeSensitiveAuthenticationRequired)) {
- values.timeSensitiveAuthenticationRequired[1]() // Signal failure/dismissal
- }
posthog.capture('reauthentication_modal_dismissed')
+ actions.resolveSensitiveAction('failure')
}
},
showSsoReauthenticationError: () => {
diff --git a/frontend/src/lib/logic/apiStatusLogic.ts b/frontend/src/lib/logic/apiStatusLogic.ts
index 8d55e5391962..41afd6ba955b 100644
--- a/frontend/src/lib/logic/apiStatusLogic.ts
+++ b/frontend/src/lib/logic/apiStatusLogic.ts
@@ -1,4 +1,5 @@
import { MakeLogicType, actions, kea, listeners, path, reducers } from 'kea'
+import posthog from 'posthog-js'
import { lemonToast } from '@posthog/lemon-ui'
@@ -23,6 +24,9 @@ export interface apiStatusLogicActions {
error: any
response: Response | undefined
}
+ resolveSensitiveAction: (outcome: 'failure' | 'success') => {
+ outcome: 'failure' | 'success'
+ }
setInternetConnectionIssue: (issue: boolean) => {
issue: boolean
}
@@ -43,6 +47,7 @@ export const apiStatusLogic = kea<apiStatusLogicType>([
path(['lib', 'apiStatusLogic']),
actions({
onApiResponse: (response?: Response, error?: any) => ({ response, error }),
+ resolveSensitiveAction: (outcome: 'success' | 'failure') => ({ outcome }),
setInternetConnectionIssue: (issue: boolean) => ({ issue }),
setTimeSensitiveAuthenticationRequired: (
required: boolean | [onSuccess: () => void, onFailure: () => void]
@@ -86,6 +91,22 @@ export const apiStatusLogic = kea<apiStatusLogicType>([
],
}),
listeners(({ cache, actions, values }) => ({
+ resolveSensitiveAction: ({ outcome }) => {
+ const pending = values.timeSensitiveAuthenticationRequired
+ if (outcome === 'success') {
+ // Clear the gate first. A callback that throws is the caller's own bug, and it must
+ // not leave the user behind the re-authentication modal with the action blocked.
+ actions.setTimeSensitiveAuthenticationRequired(false)
+ }
+ if (!Array.isArray(pending)) {
+ return
+ }
+ try {
+ pending[outcome === 'success' ? 0 : 1]()
+ } catch (e) {
+ posthog.captureException(e)
+ }
+ },
onApiResponse: async ({ response, error }, breakpoint) => {
if (error || !response?.status) {
await breakpoint(50)
diff --git a/frontend/src/scenes/authentication/shared/passkeyLogic.test.ts b/frontend/src/scenes/authentication/shared/passkeyLogic.test.ts
index e41339880e1d..54ca96d14527 100644
--- a/frontend/src/scenes/authentication/shared/passkeyLogic.test.ts
+++ b/frontend/src/scenes/authentication/shared/passkeyLogic.test.ts
@@ -143,5 +143,19 @@ describe('passkeyLogic', () => {
expect(onFailure).not.toHaveBeenCalled()
expect(apiStatusLogic.values.timeSensitiveAuthenticationRequired).toBe(false)
})
+
+ it('clears the gate even when the sensitive action throws', async () => {
+ const onSuccess = jest.fn(() => {
+ throw new Error('sensitive action failed')
+ })
+ apiStatusLogic.actions.setTimeSensitiveAuthenticationRequired([onSuccess, jest.fn()])
+
+ logic.actions.beginPasskeyLogin(undefined, { reauth: 'true' })
+ await expectLogic(logic).toFinishAllListeners()
+
+ expect(onSuccess).toHaveBeenCalledTimes(1)
+ expect(apiStatusLogic.values.timeSensitiveAuthenticationRequired).toBe(false)
+ expect(logic.values.isReauth).toBe(false)
+ })
})
})
diff --git a/frontend/src/scenes/authentication/shared/passkeyLogic.ts b/frontend/src/scenes/authentication/shared/passkeyLogic.ts
index d898d4ef6b51..5ababbddc9b7 100644
--- a/frontend/src/scenes/authentication/shared/passkeyLogic.ts
+++ b/frontend/src/scenes/authentication/shared/passkeyLogic.ts
@@ -46,7 +46,6 @@ function routeToEmailVerification(e: unknown): boolean {
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface passkeyLogicValues {
- timeSensitiveAuthenticationRequired: boolean | [onSuccess: () => void, onFailure: () => void] // apiStatusLogic
allowCredentialsFromPrecheck: PublicKeyCredentialDescriptorJSON[]
isLoading: boolean
isReauth: boolean
@@ -58,8 +57,8 @@ export interface passkeyLogicValues {
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface passkeyLogicActions {
- setTimeSensitiveAuthenticationRequired: (required: boolean | [onSuccess: () => void, onFailure: () => void]) => {
- required: boolean | [onSuccess: () => void, onFailure: () => void]
+ resolveSensitiveAction: (outcome: 'failure' | 'success') => {
+ outcome: 'failure' | 'success'
} // apiStatusLogic
setGeneralError: (
code: string,
@@ -115,15 +114,7 @@ export type passkeyLogicType = MakeLogicType<passkeyLogicValues, passkeyLogicAct
export const passkeyLogic = kea<passkeyLogicType>([
path(['scenes', 'authentication', 'shared', 'passkeyLogic']),
connect(() => ({
- values: [apiStatusLogic, ['timeSensitiveAuthenticationRequired']],
- actions: [
- loginLogic,
- ['setGeneralError'],
- apiStatusLogic,
- ['setTimeSensitiveAuthenticationRequired'],
- userLogic,
- ['loadUser'],
- ],
+ actions: [loginLogic, ['setGeneralError'], apiStatusLogic, ['resolveSensitiveAction'], userLogic, ['loadUser']],
})),
actions({
beginPasskeyLogin: (
@@ -280,11 +271,8 @@ export const passkeyLogic = kea<passkeyLogicType>([
}
// for reauth, clear authentication required flag
if (values.isReauth) {
- if (Array.isArray(values.timeSensitiveAuthenticationRequired)) {
- values.timeSensitiveAuthenticationRequired[0]() // Signal success
- }
posthog.capture('reauthentication_completed', { method: 'passkey' })
- actions.setTimeSensitiveAuthenticationRequired(false)
+ actions.resolveSensitiveAction('success')
actions.loadUser()
actions.reset()
return