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(data-warehouse): use the account just authorized in the source wizard (#108878)

Tom Owers453eddddac0d
diff --git a/frontend/src/lib/integrations/integrationsLogic.ts b/frontend/src/lib/integrations/integrationsLogic.ts
index 7e746f1beeaf..a3e0c0cad1af 100644
--- a/frontend/src/lib/integrations/integrationsLogic.ts
+++ b/frontend/src/lib/integrations/integrationsLogic.ts
@@ -65,6 +65,11 @@ function landsOnErrorAwarePage(pathname: string): boolean {
return segments.length === 2 && segments[0] === 'data-warehouse' && segments[1] === 'new-source'
}
+// Search param the OAuth callback appends with the id of the integration it just created, so the
+// page the user lands back on can select the connection they authorized rather than guessing.
+// pinned: URL search param — the callback redirect and the landing page both read it.
+export const OAUTH_INTEGRATION_ID_PARAM = 'integration_id'
+
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface integrationsLogicValues {
preflight: PreflightStatus | null // preflightLogic
@@ -1254,7 +1259,7 @@ export const integrationsLogic = kea<integrationsLogicType>([
// Add the integration ID to the replaceUrl so that the landing page can use it
const url = new URL(replaceUrl, window.location.origin)
- url.searchParams.set('integration_id', String(integration.id))
+ url.searchParams.set(OAUTH_INTEGRATION_ID_PARAM, String(integration.id))
replaceUrl = url.pathname + url.search + url.hash
actions.loadIntegrations()
diff --git a/products/data_warehouse/frontend/shared/components/forms/IntegrationChoice.test.ts b/products/data_warehouse/frontend/shared/components/forms/IntegrationChoice.test.ts
new file mode 100644
index 000000000000..2728f5771f0e
--- /dev/null
+++ b/products/data_warehouse/frontend/shared/components/forms/IntegrationChoice.test.ts
@@ -0,0 +1,24 @@
+import { IntegrationType } from '~/types'
+
+import { authorizedIntegrationId } from './IntegrationChoice'
+
+const integration = (id: number, kind: string): IntegrationType => ({ id, kind }) as IntegrationType
+
+describe('SourceIntegrationChoice', () => {
+ // Reconnecting mid-setup leaves the field pointing at the replaced connection, which blocks the
+ // wizard on the account step with no way forward but starting over.
+ it.each([
+ ['adopts the connection the callback just created', { integration_id: '7' }, 7],
+ ['ignores a connection belonging to another provider', { integration_id: '8' }, null],
+ ['ignores an id no connection matches', { integration_id: '99' }, null],
+ ['ignores a blank param', { integration_id: '' }, null],
+ ['ignores a missing param', {}, null],
+ ])('%s', (_name, searchParams, expected) => {
+ const integrations = [integration(7, 'linkedin-ads'), integration(8, 'google-ads')]
+ expect(authorizedIntegrationId(searchParams, integrations, 'linkedin-ads')).toEqual(expected)
+ })
+
+ it('waits for the connection list rather than adopting an unverified id', () => {
+ expect(authorizedIntegrationId({ integration_id: '7' }, null, 'linkedin-ads')).toBeNull()
+ })
+})
diff --git a/products/data_warehouse/frontend/shared/components/forms/IntegrationChoice.tsx b/products/data_warehouse/frontend/shared/components/forms/IntegrationChoice.tsx
index f679113788b1..d5766ee684dd 100644
--- a/products/data_warehouse/frontend/shared/components/forms/IntegrationChoice.tsx
+++ b/products/data_warehouse/frontend/shared/components/forms/IntegrationChoice.tsx
@@ -1,14 +1,18 @@
import { useActions, useValues } from 'kea'
import { router } from 'kea-router'
+import { useEffect, useRef } from 'react'
import {
IntegrationChoice,
IntegrationConfigureProps,
} from 'lib/components/CyclotronJob/integrations/IntegrationChoice'
+import { integrationsLogic, OAUTH_INTEGRATION_ID_PARAM } from 'lib/integrations/integrationsLogic'
import { describeOAuthCallbackError, INTEGRATION_ERROR_PARAM } from 'lib/integrations/oauthCallbackErrors'
import { LemonBanner } from 'lib/lemon-ui/LemonBanner'
import { urls } from 'scenes/urls'
+import { IntegrationType } from '~/types'
+
import { SourceConfigResponseApi } from 'products/warehouse_sources/frontend/generated/api.schemas'
import { sourceWizardLogic } from '../../../scenes/NewSourceScene/sourceWizardLogic'
@@ -17,19 +21,61 @@ export type SourceIntegrationChoiceProps = IntegrationConfigureProps & {
sourceConfig: SourceConfigResponseApi
}
+/** The connection the OAuth callback just created, when it is one this field can use. The callback
+ * appends its id to the URL it returns to; a field that ignores it keeps whatever it held before
+ * the round-trip, which is the connection a reconnecting user just replaced. */
+export function authorizedIntegrationId(
+ searchParams: Record<string, any>,
+ integrations: IntegrationType[] | null,
+ kind: string
+): number | null {
+ const id = Number(searchParams[OAUTH_INTEGRATION_ID_PARAM])
+ if (!Number.isInteger(id) || id <= 0) {
+ return null
+ }
+ // Any kind's callback can land on this URL, and holding another provider's connection here
+ // would only surface later as a failed schema discovery.
+ return integrations?.some((integration) => integration.id === id && integration.kind === kind) ? id : null
+}
+
export function SourceIntegrationChoice({
sourceConfig,
integration,
+ value,
+ onChange,
...props
}: SourceIntegrationChoiceProps): JSX.Element {
const { saveFormStateBeforeRedirect } = useActions(sourceWizardLogic)
const { location, searchParams } = useValues(router)
+ const { integrations, integrationsLoading } = useValues(integrationsLogic)
const sourceKind = sourceConfig.name.toLowerCase()
+ const kind = integration ?? sourceKind
// A failed authorization sends the user back here, where the connect button is. The callback
// carries the reason in the URL rather than a toast, which would be gone before they retry.
const oauthError = searchParams[INTEGRATION_ERROR_PARAM]
+ const adoptedAuthorizedIntegration = useRef(false)
+ useEffect(() => {
+ if (adoptedAuthorizedIntegration.current || integrationsLoading) {
+ return
+ }
+ const authorizedId = authorizedIntegrationId(searchParams, integrations, kind)
+ if (authorizedId === null) {
+ return
+ }
+ adoptedAuthorizedIntegration.current = true
+ if (value !== authorizedId) {
+ onChange?.(authorizedId)
+ }
+ // Consume the one-shot param, so remounting this field later can't override an account the
+ // user picked by hand after coming back.
+ const params = new URLSearchParams(location.search)
+ params.delete(OAUTH_INTEGRATION_ID_PARAM)
+ const query = params.toString()
+ router.actions.replace(`${location.pathname}${query ? `?${query}` : ''}${location.hash}`)
+ }, [integrations, integrationsLoading, searchParams, kind, value, onChange, location])
+
// In onboarding the wizard is embedded in the page. A full-page OAuth redirect to the
// standalone new-source scene would drop the user out of the onboarding flow, so when we're
// on an onboarding route we return to the current onboarding URL with the source kind instead.
@@ -52,7 +98,9 @@ export function SourceIntegrationChoice({
)}
<IntegrationChoice
{...props}
- integration={integration ?? sourceKind}
+ value={value}
+ onChange={onChange}
+ integration={kind}
redirectUrl={redirectUrl}
beforeRedirect={saveFormStateBeforeRedirect}
/>