: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
import { decode } from 'he'
import { LogicWrapper, MakeLogicType, actions, afterMount, kea, path, reducers } from 'kea'
import { getCookie } from 'lib/api'
import { tryJsonParse } from 'lib/utils/json'
// pinned: the backend sets this cookie (posthog/helpers/oauth_pending_connection.py) and the website reads it
export const PENDING_OAUTH_CONNECTION_COOKIE = 'ph_pending_oauth_connection'
/** The OAuth application a signed-out visitor was asked to authorize before they reached login or signup. */
export interface PendingOAuthConnection {
clientName: string
clientId: string
logoUri: string | null
/** Host the person returns to when the connection completes, when the client registered a web redirect. */
redirectHost: string | null
}
function optionalString(value: unknown): string | null {
return typeof value === 'string' && value ? value : null
}
export function readPendingOAuthConnection(): PendingOAuthConnection | null {
const raw = getCookie(PENDING_OAUTH_CONNECTION_COOKIE)
if (!raw) {
return null
}
const parsed = tryJsonParse(raw, null)
if (!parsed || typeof parsed !== 'object') {
return null
}
const payload = parsed as Record<string, unknown>
const clientName = optionalString(payload.client_name)
const clientId = optionalString(payload.client_id)
if (!clientName || !clientId) {
return null
}
const logoUri = optionalString(payload.logo_uri)
return {
// The name is HTML-escaped at ingestion (posthog/api/oauth/client_name.py). Decode it so
// React's own output escaping renders "&" instead of "&".
clientName: decode(clientName),
clientId,
// Only a web image can render on an https page, so data: and http: URIs are dropped.
logoUri: logoUri?.startsWith('https://') ? logoUri : null,
redirectHost: optionalString(payload.redirect_host),
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface pendingOAuthConnectionLogicValues {
pendingConnection: PendingOAuthConnection | null
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface pendingOAuthConnectionLogicActions {
clearPendingConnection: () => {
value: true
}
setPendingConnection: (pendingConnection: PendingOAuthConnection) => {
pendingConnection: PendingOAuthConnection
}
}
export type pendingOAuthConnectionLogicType = MakeLogicType<
pendingOAuthConnectionLogicValues,
pendingOAuthConnectionLogicActions
>
/** One instance for the whole auth flow, so a step change inside a scene does not re-read the cookie. */
export const pendingOAuthConnectionLogic: LogicWrapper<pendingOAuthConnectionLogicType> =
kea<pendingOAuthConnectionLogicType>([
path(['scenes', 'authentication', 'shared', 'pendingOAuthConnectionLogic']),
actions({
setPendingConnection: (pendingConnection: PendingOAuthConnection) => ({ pendingConnection }),
clearPendingConnection: true,
}),
reducers({
pendingConnection: [
null as PendingOAuthConnection | null,
{
setPendingConnection: (_, { pendingConnection }) => pendingConnection,
clearPendingConnection: () => null,
},
],
}),
afterMount(({ actions }) => {
const pendingConnection = readPendingOAuthConnection()
if (!pendingConnection) {
return
}
actions.setPendingConnection(pendingConnection)
}),
])