: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): route passkey login on unverified accounts to code entry (#91690)
Alex Lider46b35e0e2360
diff --git a/frontend/src/scenes/authentication/shared/passkeyLogic.ts b/frontend/src/scenes/authentication/shared/passkeyLogic.tsindex 6fd6524a7d8a..bea1fdd5295f 100644--- a/frontend/src/scenes/authentication/shared/passkeyLogic.ts+++ b/frontend/src/scenes/authentication/shared/passkeyLogic.ts@@ -8,10 +8,12 @@ import { loaders } from 'kea-loaders'import { router } from 'kea-router'import api from 'lib/api'+import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'import { apiStatusLogic } from 'lib/logic/apiStatusLogic'import { isWebKitBrowser } from 'lib/utils/dom'import { handleLoginRedirect, loginLogic } from 'scenes/authentication/login/loginLogic'import { getPasskeyErrorMessage, isWebAuthnCancellation } from 'scenes/settings/user/passkeys/utils'+import { urls } from 'scenes/urls'import { userLogic } from 'scenes/userLogic'export interface PasskeyLoginBeginResponse {@@ -28,6 +30,19 @@ export interface BeginPasskeyLoginParams {reauth?: 'true' | 'false'}+// A login on an unverified account fails with code `verify_email_pending` and the user+// uuid in `detail`. The page at /verify_email/<uuid> has the code entry form.+function routeToEmailVerification(e: unknown): boolean {+ const { code, detail } = (e ?? {}) as Record<string, any>+ if (code !== 'verify_email_pending' || typeof detail !== 'string') {+ return false+ }+ lemonToast.info('Verify your email to continue. We just sent you a new code.')+ const next: string | undefined = router.values.searchParams.next+ router.actions.push(urls.verifyEmail(detail), next ? { next } : {})+ return true+}+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.export interface passkeyLogicValues {allowCredentialsFromPrecheck: PublicKeyCredentialDescriptorJSON[]@@ -191,6 +206,11 @@ export const passkeyLogic = kea<passkeyLogicType>([actions.passkeyAuthenticationCancelled()return null}+ if (routeToEmailVerification(e)) {+ // Throw so the failure listener resets the passkey state. The user+ // continues on the verification page, not with a login error.+ throw e+ }actions.setGeneralError('passkey_error', getPasskeyErrorMessage(e))throw e}@@ -242,7 +262,7 @@ export const passkeyLogic = kea<passkeyLogicType>([} catch (e: unknown) {// The autofill passkey prompt is routinely dismissed — the user types a password// instead, or navigates away. Swallow those; surface anything genuinely wrong.- if (!isWebAuthnCancellation(e)) {+ if (!isWebAuthnCancellation(e) && !routeToEmailVerification(e)) {actions.setGeneralError('passkey_error', getPasskeyErrorMessage(e))}}diff --git a/posthog/api/test/test_webauthn.py b/posthog/api/test/test_webauthn.pyindex 64d34a80c943..d850b4ce095b 100644--- a/posthog/api/test/test_webauthn.py+++ b/posthog/api/test/test_webauthn.py@@ -270,8 +270,11 @@ class TestWebAuthnLogin(APIBaseTest):},format="json",)- self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)- self.assertIn("awaiting verification", response.json()["error"].lower())+ # The contract is the same as the password login path: the frontend uses the+ # uuid to route to the code entry page at /verify_email/<uuid>.+ self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)+ self.assertEqual(response.json()["code"], "verify_email_pending")+ self.assertEqual(response.json()["detail"], str(self.user.uuid))me_response = self.client.get("/api/users/@me/")self.assertEqual(me_response.status_code, status.HTTP_401_UNAUTHORIZED)diff --git a/posthog/api/webauthn.py b/posthog/api/webauthn.pyindex d054aa0087f1..40c04a6d2540 100644--- a/posthog/api/webauthn.py+++ b/posthog/api/webauthn.py@@ -17,7 +17,7 @@ from webauthn.helpers import base64url_to_bytes, bytes_to_base64url, options_to_from webauthn.helpers.decode_credential_public_key import decode_credential_public_keyfrom webauthn.helpers.structs import AuthenticatorTransport, PublicKeyCredentialDescriptor-from posthog.api.authentication import axes_locked_out, is_email_verified_for_login+from posthog.api.authentication import EmailVerificationPending, axes_locked_out, is_email_verified_for_loginfrom posthog.auth import SessionAuthentication, WebAuthnAuthenticationResponse, WebauthnBackendfrom posthog.event_usage import report_user_logged_infrom posthog.helpers.two_factor_session import set_two_factor_verified_in_session@@ -362,14 +362,10 @@ class WebAuthnLoginViewSet(viewsets.ViewSet):)if not is_email_verified_for_login(verified_user):- return Response(- {- "error": (- "Your account is awaiting verification. Log in with your password to enter the code we emailed you."- )- },- status=status.HTTP_400_BAD_REQUEST,- )+ # The passkey assertion is verified at this point, so the uuid is safe to return:+ # the password path returns the same uuid after a correct password. The frontend+ # uses the uuid to route to /verify_email/<uuid>.+ raise EmailVerificationPending(str(verified_user.uuid))# Login the user with the WebauthnBackendlogin(request, verified_user, backend="posthog.auth.WebauthnBackend")@@ -386,6 +382,10 @@ class WebAuthnLoginViewSet(viewsets.ViewSet):except AxesBackendPermissionDenied:return axes_locked_out(request)+ except EmailVerificationPending:+ # The DRF handler formats this as a 401 with the user uuid, the same+ # response as the password login path.+ raiseexcept Exception as e:logger.exception("webauthn_login_error", error=str(e))return Response(