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.

feat(auth): precheck the login email when typing pauses (#113841)

Yasen18b635dfeae5
diff --git a/frontend/src/scenes/authentication/login/loginLogic.test.ts b/frontend/src/scenes/authentication/login/loginLogic.test.ts
index 4d9a0df51d89..00a583798bcf 100644
--- a/frontend/src/scenes/authentication/login/loginLogic.test.ts
+++ b/frontend/src/scenes/authentication/login/loginLogic.test.ts
@@ -153,13 +153,17 @@ describe('loginLogic', () => {
jest.clearAllMocks()
})
- it('auto-triggers the passkey prompt on non-WebKit browsers', async () => {
+ it('auto-triggers the passkey prompt once per email on non-WebKit browsers', async () => {
logic.actions.precheck({ email: '[email protected]' })
// Drain the whole passkey flow (begin request included) so nothing leaks into the next test.
await expectLogic(passkeyLogic)
.toDispatchActions(['beginPasskeyLogin', 'startPasskeyAuthenticationSuccess'])
.toFinishAllListeners()
expect(beginHandler).toHaveBeenCalledTimes(1)
+
+ logic.actions.precheck({ email: '[email protected]', autoAttempt: true })
+ await expectLogic(logic).toDispatchActions(['precheckSuccess']).toFinishAllListeners()
+ expect(beginHandler).toHaveBeenCalledTimes(1)
})
it('does not auto-trigger the passkey modal on WebKit (Safari)', async () => {
@@ -284,6 +288,24 @@ describe('loginLogic', () => {
expect(precheckHandler).toHaveBeenCalledTimes(2)
})
+ it('prechecks the latest email only after typing pauses for 3 seconds', async () => {
+ jest.useFakeTimers()
+ try {
+ logic.actions.setLoginValue('email', '[email protected]')
+ await jest.advanceTimersByTimeAsync(2999)
+ logic.actions.setLoginValue('email', '[email protected]')
+ await jest.advanceTimersByTimeAsync(2999)
+ expect(precheckHandler).not.toHaveBeenCalled()
+
+ await jest.advanceTimersByTimeAsync(1)
+ expect(precheckHandler).toHaveBeenCalledTimes(1)
+ await jest.advanceTimersByTimeAsync(0)
+ expect(logic.values.precheckResponse).toMatchObject({ status: 'completed', email: '[email protected]' })
+ } finally {
+ jest.useRealTimers()
+ }
+ })
+
it('retries a failed precheck instead of caching its fallback for the page session', async () => {
precheckHandler.mockImplementationOnce(() => [429, { detail: 'Request was throttled.' }])
diff --git a/frontend/src/scenes/authentication/login/loginLogic.ts b/frontend/src/scenes/authentication/login/loginLogic.ts
index 556ce3c883dc..3ec44e4df15d 100644
--- a/frontend/src/scenes/authentication/login/loginLogic.ts
+++ b/frontend/src/scenes/authentication/login/loginLogic.ts
@@ -14,7 +14,7 @@ import { lemonToast } from 'lib/lemon-ui/LemonToast'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { isWebKitBrowser } from 'lib/utils/dom'
import { getCurrentTeamIdOrNone } from 'lib/utils/getAppContext'
-import { getRelativeNextPath } from 'lib/utils/url'
+import { getRelativeNextPath, isEmail } from 'lib/utils/url'
import { devLoginLogic } from 'scenes/authentication/shared/devLoginLogic'
import {
clearPendingVerificationEmail,
@@ -209,6 +209,7 @@ export interface loginLogicValues {
loginTouched: boolean
loginTouches: Record<string, boolean>
loginValidationErrors: DeepPartialMap<LoginForm, ValidationErrorType>
+ passkeyPromptedForEmail: string | null
precheckResponse: PrecheckResponseType
precheckResponseLoading: boolean
resendResponse: {
@@ -235,6 +236,9 @@ export interface loginLogicActions {
exitCodeVerification: () => {
value: true
}
+ markPasskeyPrompted: (email: string) => {
+ email: string
+ }
precheck: ({ email }: { autoAttempt?: boolean; email: string }) => {
email: string
autoAttempt?: boolean
@@ -390,6 +394,8 @@ export interface loginLogicMeta {
export type loginLogicType = MakeLogicType<loginLogicValues, loginLogicActions, Record<string, any>, loginLogicMeta>
+const PRECHECK_IDLE_DELAY_MS = 3000
+
export const loginLogic = kea<loginLogicType>([
path(['scenes', 'authentication', 'login', 'loginLogic']),
connect(() => ({
@@ -409,6 +415,7 @@ export const loginLogic = kea<loginLogicType>([
setCodeVerificationRequired: (email: string) => ({ email }),
exitCodeVerification: true,
startAutoRedirectToProvider: (provider: SSOProvider, email: string) => ({ provider, email }),
+ markPasskeyPrompted: (email: string) => ({ email }),
}),
reducers({
// This is separate from the login form, so that the form can be submitted even if a general error is present
@@ -452,6 +459,12 @@ export const loginLogic = kea<loginLogicType>([
startAutoRedirectToProvider: (_, { email }) => email,
},
],
+ passkeyPromptedForEmail: [
+ null as string | null,
+ {
+ markPasskeyPrompted: (_, { email }) => email,
+ },
+ ],
}),
loaders(({ values }) => ({
precheckResponse: [
@@ -676,6 +689,18 @@ export const loginLogic = kea<loginLogicType>([
exitCodeVerification: () => {
actions.resetCodeVerification()
},
+ // Precheck once typing pauses, so SSO options show without a blur. No `autoAttempt`: a pause
+ // is not an explicit gesture, so it must not bounce the user out to an identity provider.
+ setLoginValue: async ({ name }, breakpoint) => {
+ if (name !== 'email') {
+ return
+ }
+ await breakpoint(PRECHECK_IDLE_DELAY_MS)
+ const { email } = values.login
+ if (isEmail(email, { requireTLD: true })) {
+ actions.precheck({ email })
+ }
+ },
// Manual errors persist until the next submit, so clear the invalid-code error as soon as
// the user edits the code, like the signup verify screen does
setCodeVerificationValue: () => {
@@ -693,10 +718,17 @@ export const loginLogic = kea<loginLogicType>([
!precheckResponse.sso_enforcement &&
!isWebKitBrowser()
) {
+ const promptEmail = precheckResponse.email ?? ''
+ if (values.passkeyPromptedForEmail === promptEmail) {
+ return
+ }
breakpoint()
// Dynamic import to avoid circular dependency
const { passkeyLogic } = await import('scenes/authentication/shared/passkeyLogic')
breakpoint()
+ // Mark only after the last breakpoint. A newer run that starts during the import cancels
+ // this one, so the email must still be unmarked when the newer run checks it.
+ actions.markPasskeyPrompted(promptEmail)
passkeyLogic.actions.beginPasskeyLogin(precheckResponse.webauthn_credentials)
return
}