: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 clsx from 'clsx'
import { useValues } from 'kea'
import { router } from 'kea-router'
import { Component, type ReactNode, Suspense } from 'react'
import { SSO_PROVIDER_NAMES } from 'lib/constants'
import { LemonButton, LemonButtonWithoutSideActionProps } from 'lib/lemon-ui/LemonButton'
import { LemonDivider } from 'lib/lemon-ui/LemonDivider'
import { LemonTag } from 'lib/lemon-ui/LemonTag'
import { preflightLogic } from 'lib/logic/preflightLogic'
import { isChunkLoadError } from 'lib/utils/isChunkLoadError'
import { lazyWithRetry } from 'lib/utils/retryImport'
import { LoginMethod, SSOProvider } from '~/types'
import passkeyLogo from './passkey.svg'
import { SocialLoginIcon } from './SocialLoginIcon'
import { getSocialLoginUrl } from './socialLoginUrl'
// The passkey flow pulls in the login logic and the WebAuthn browser library.
const PasskeyLoginButton = lazyWithRetry(() =>
import('scenes/authentication/shared/PasskeyLoginButton').then((m) => ({ default: m.PasskeyLoginButton }))
)
interface SocialLoginLinkProps {
provider: SSOProvider
extraQueryParams?: Record<string, string>
onProviderClick?: (provider: SSOProvider) => void
children: JSX.Element
}
function SocialLoginLink({ provider, extraQueryParams, onProviderClick, children }: SocialLoginLinkProps): JSX.Element {
const { searchParams } = useValues(router)
const loginUrl = getSocialLoginUrl(provider, extraQueryParams, searchParams)
const iframed = window !== window.parent
return (
// eslint-disable-next-line react/forbid-elements
<a
className="block"
href={loginUrl}
onClick={
onProviderClick
? (event) => {
event.preventDefault()
onProviderClick(provider)
}
: undefined
}
{...(iframed && !onProviderClick ? { target: '_blank', rel: 'noopener' } : {})}
>
{children}
</a>
)
}
interface SocialLoginButtonProps {
provider: SSOProvider
extraQueryParams?: Record<string, string>
onProviderClick?: (provider: SSOProvider) => void
isLastUsed?: boolean
}
export function SocialLoginButton({
provider,
extraQueryParams,
onProviderClick,
isLastUsed,
}: SocialLoginButtonProps): JSX.Element | null {
const { preflight } = useValues(preflightLogic)
if (!preflight?.available_social_auth_providers[provider]) {
return null
}
return (
<SocialLoginLink provider={provider} extraQueryParams={extraQueryParams} onProviderClick={onProviderClick}>
<div className="relative">
<LemonButton
size="large"
icon={<SocialLoginIcon provider={provider} />}
active={isLastUsed}
tooltip={SSO_PROVIDER_NAMES[provider]}
/>
{isLastUsed && (
<LemonTag
type="muted"
size="small"
className="absolute -top-3 left-1/2 -translate-x-1/2 pointer-events-none"
>
Last used
</LemonTag>
)}
</div>
</SocialLoginLink>
)
}
/** Holds the passkey button's place while its chunk loads, or when the chunk fails to load. */
function PasskeyButtonPlaceholder({
loading,
disabledReason,
}: {
loading?: boolean
disabledReason?: string
}): JSX.Element {
return (
<LemonButton
size="large"
icon={<img src={passkeyLogo} alt="Passkey" className="object-contain w-7 h-7" />}
tooltip={disabledReason ? undefined : 'Passkey'}
htmlType="button"
loading={loading}
disabledReason={disabledReason}
/>
)
}
/** The re-authentication modal also shows the passkey button, and a page reload there would discard
* the user's work. So a failed chunk load disables the button in place instead of reloading. */
class PasskeyChunkLoadBoundary extends Component<{ children: ReactNode }, { error: unknown }> {
override state: { error: unknown } = { error: null }
static getDerivedStateFromError(error: unknown): { error: unknown } {
return { error }
}
override render(): ReactNode {
if (!this.state.error) {
return this.props.children
}
if (!isChunkLoadError(this.state.error)) {
throw this.state.error
}
return (
<PasskeyButtonPlaceholder disabledReason="Passkey sign-in couldn't load. Refresh the page to try again, or use another sign-in method." />
)
}
}
interface SocialLoginButtonsProps {
title?: string
caption?: string
captionLocation?: 'top' | 'bottom'
className?: string
topDivider?: boolean
bottomDivider?: boolean
extraQueryParams?: Record<string, string>
onProviderClick?: (provider: SSOProvider) => void
lastUsedProvider?: LoginMethod
showPasskey?: boolean
/**
* Limit the rendered providers to this allowlist. Used when we know which providers a specific
* account can actually use, so we don't offer a button that would fail. `null`/omitted means
* "no restriction" — render everything the instance has configured.
*
* Note `'saml'` is never rendered here (it isn't in `available_social_auth_providers`); it goes
* through `SSOEnforcedLoginButton` instead.
*/
restrictToProviders?: SSOProvider[] | null
/** Rendered at the end of the row, after the providers and the passkey. */
extraButton?: ReactNode
}
export function SocialLoginButtons({
title,
caption,
captionLocation = 'top',
className,
topDivider,
bottomDivider,
lastUsedProvider,
showPasskey = false,
restrictToProviders,
onProviderClick,
extraButton,
...props
}: SocialLoginButtonsProps): JSX.Element | null {
const { preflight, socialAuthAvailable } = useValues(preflightLogic)
if (!preflight || (!socialAuthAvailable && !showPasskey && !extraButton)) {
return null
}
const order: string[] = Object.keys(SSO_PROVIDER_NAMES)
const socialProviders = socialAuthAvailable
? Object.keys(preflight.available_social_auth_providers)
.filter((provider) => !restrictToProviders || restrictToProviders.includes(provider as SSOProvider))
.sort((a, b) => order.indexOf(a) - order.indexOf(b))
: []
if (!socialProviders.length && !showPasskey && !extraButton) {
return null
}
return (
<>
{topDivider ? <LemonDivider dashed className="my-4" /> : null}
<div className={clsx(className, 'text-center deprecated-space-y-4')}>
{title && <h3>{title}</h3>}
{caption && captionLocation === 'top' && <p className="text-secondary">{caption}</p>}
<div className="flex gap-4 justify-center flex-wrap">
{socialProviders.map((provider) => (
<SocialLoginButton
key={provider}
provider={provider as SSOProvider}
isLastUsed={lastUsedProvider === provider}
onProviderClick={onProviderClick}
{...props}
/>
))}
{showPasskey && (
<PasskeyChunkLoadBoundary>
<Suspense fallback={<PasskeyButtonPlaceholder loading />}>
<PasskeyLoginButton isLastUsed={lastUsedProvider === 'passkey'} {...props} />
</Suspense>
</PasskeyChunkLoadBoundary>
)}
{extraButton}
</div>
{caption && captionLocation === 'bottom' && <p className="text-secondary">{caption}</p>}
</div>
{bottomDivider ? <LemonDivider dashed className="my-6" /> : null}
</>
)
}
type SSOEnforcedLoginButtonProps = SocialLoginButtonProps &
Partial<LemonButtonWithoutSideActionProps> & {
email: string
} & {
actionText?: string
}
export function SSOEnforcedLoginButton({
provider,
email,
extraQueryParams,
onProviderClick,
actionText = 'Log in',
isLastUsed,
...props
}: SSOEnforcedLoginButtonProps): JSX.Element {
return (
<SocialLoginLink
provider={provider}
extraQueryParams={{ ...extraQueryParams, email }}
onProviderClick={onProviderClick}
>
<LemonButton
className="btn-bridge relative"
data-attr="sso-login"
htmlType="button"
type="secondary"
fullWidth
center
icon={<SocialLoginIcon provider={provider} />}
size="large"
{...props}
>
{actionText} with {SSO_PROVIDER_NAMES[provider]}
{isLastUsed && (
<LemonTag type="muted" size="medium" className="absolute -top-3 -right-2 pointer-events-none">
Last used
</LemonTag>
)}
</LemonButton>
</SocialLoginLink>
)
}