: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
feat(auth): only say "welcome back" to a browser that has logged in (#101360)
Rafael Audibertcea129a217b5
diff --git a/frontend/src/scenes/authentication/login/Login.stories.tsx b/frontend/src/scenes/authentication/login/Login.stories.tsxindex bf8799436bbf..a9452f968e31 100644--- a/frontend/src/scenes/authentication/login/Login.stories.tsx+++ b/frontend/src/scenes/authentication/login/Login.stories.tsx@@ -1,4 +1,5 @@import { WEBSITE_REFERRER, setDocumentReferrer } from 'scenes/authentication/shared/authReferrer.mock'+import { setLastLoginMethodCookie } from 'scenes/authentication/shared/lastLoginMethod.mock'import {PENDING_OAUTH_CONNECTION_FIXTURE,setPendingOAuthConnectionCookie,@@ -25,6 +26,7 @@ type StoryArgs = {generalError: 'none' | 'invalid_credentials' | 'code_based_verification_sent'pendingOAuthConnection: booleanarrivedFromWebsite: boolean+ hasLoggedInBefore: boolean}const meta: Meta<StoryArgs> = {@@ -53,6 +55,7 @@ const meta: Meta<StoryArgs> = {},pendingOAuthConnection: { control: 'boolean', name: 'Pending OAuth connection' },arrivedFromWebsite: { control: 'boolean', name: 'Arrived from posthog.com' },+ hasLoggedInBefore: { control: 'boolean', name: 'Has logged in before' },},args: {cloud: true,@@ -65,6 +68,7 @@ const meta: Meta<StoryArgs> = {generalError: 'none',pendingOAuthConnection: false,arrivedFromWebsite: false,+ hasLoggedInBefore: true,},}export default meta@@ -80,11 +84,13 @@ const Template: StoryFn<StoryArgs> = ({generalError,pendingOAuthConnection,arrivedFromWebsite,+ hasLoggedInBefore,}) => {const enforcement = ssoEnforcement === 'none' ? null : ssoEnforcement// Set synchronously: the scene reads the cookie while it mounts during this same render.setPendingOAuthConnectionCookie(pendingOAuthConnection ? PENDING_OAUTH_CONNECTION_FIXTURE : null)setDocumentReferrer(arrivedFromWebsite ? WEBSITE_REFERRER : '')+ setLastLoginMethodCookie(hasLoggedInBefore ? 'password' : null)useStorybookMocks({get: {@@ -131,7 +137,7 @@ const Template: StoryFn<StoryArgs> = ({}}, [generalError])- return <Login />+ return <Login key={String(hasLoggedInBefore)} />}export const Default: StoryFn<StoryArgs> = Template.bind({})@@ -161,3 +167,7 @@ EmailVerification.args = { generalError: 'code_based_verification_sent' }export const ArrivedFromWebsite: StoryFn<StoryArgs> = Template.bind({})ArrivedFromWebsite.storyName = 'Arrived from posthog.com'ArrivedFromWebsite.args = { arrivedFromWebsite: true }++export const FirstLoginOnThisBrowser: StoryFn<StoryArgs> = Template.bind({})+FirstLoginOnThisBrowser.storyName = 'First login on this browser'+FirstLoginOnThisBrowser.args = { hasLoggedInBefore: false }diff --git a/frontend/src/scenes/authentication/login/LoginForm.tsx b/frontend/src/scenes/authentication/login/LoginForm.tsxindex 8a3c5b755f22..86a8a56faaf9 100644--- a/frontend/src/scenes/authentication/login/LoginForm.tsx+++ b/frontend/src/scenes/authentication/login/LoginForm.tsx@@ -5,7 +5,6 @@ import { useEffect } from 'react'import * as magnifyingGlassPng from '@posthog/brand/hoggies/png/magnifying-glass-1'import { IconCheckCircle } from '@posthog/icons'-import { getCookie } from 'lib/api'import { pngHoggie } from 'lib/brand/hoggies'import { SocialLoginButtons, SSOEnforcedLoginButton } from 'lib/components/SocialLoginButton/SocialLoginButton'import { supportLogic } from 'lib/components/Support/supportLogic'@@ -20,6 +19,7 @@ import { isEmail } from 'lib/utils/url'import { AuthCardTitle } from 'scenes/authentication/shared/authScene/AuthCardTitle'import { AuthScene, AuthSceneCard } from 'scenes/authentication/shared/authScene/AuthScene'import { RegionField } from 'scenes/authentication/shared/authScene/RegionField'+import { useLastLoginMethod } from 'scenes/authentication/shared/lastLoginMethod'import { ERROR_MESSAGES } from 'scenes/authentication/shared/loginErrorMessages'import { OtherRegionHint } from 'scenes/authentication/shared/OtherRegionHint'import { pendingOAuthConnectionLogic, reviewAccessCopy } from 'scenes/authentication/shared/pendingOAuthConnectionLogic'@@ -34,10 +34,14 @@ import { LoginMethod, Region, SSOProvider } from '~/types'import { loginLogic } from './loginLogic'import { SessionRiskBanner } from './SessionRiskBanner'-const LAST_LOGIN_METHOD_COOKIE = 'ph_last_login_method'-const HedgehogMagnifyingGlass = pngHoggie(magnifyingGlassPng)+function loginGreeting(isReturning: boolean): { note: string; sub: string } {+ return isReturning+ ? { note: '// welcome back', sub: "Welcome back. Let's go ship something." }+ : { note: '// hey, good to see you', sub: "Let's go ship something." }+}+function loginMethodLabel(method: LoginMethod): string {if (method === 'password') {return 'password'@@ -125,7 +129,8 @@ export function LoginForm(): JSX.Element {const isPasswordHidden = !!precheckResponse.sso_enforcement || isPasswordLoginUnavailableconst isCodeSent = codeVerificationRequired- const lastLoginMethod = getCookie(LAST_LOGIN_METHOD_COOKIE) as LoginMethod+ const lastLoginMethod = useLastLoginMethod()+ const greeting = loginGreeting(lastLoginMethod !== null)const prevEmail = usePrevious(login.email)useEffect(() => {@@ -151,7 +156,7 @@ export function LoginForm(): JSX.Element {)return (- <AuthScene notes={['// welcome back', '// 500,000+ teams ship here']}>+ <AuthScene notes={[greeting.note, '// 500,000+ teams ship here']}>{preflight?.cloud && <RedirectIfLoggedInOtherInstance />}<AuthSceneCard footer={footer}>{isCodeSent && <HedgehogMagnifyingGlass className="block w-auto mx-auto mb-3 h-28" />}@@ -180,7 +185,7 @@ export function LoginForm(): JSX.Element {) : pendingConnection ? (reviewAccessCopy(pendingConnection, 'After you log in')) : (- "Welcome back. Let's go ship something."+ greeting.sub)}/>diff --git a/frontend/src/scenes/authentication/shared/lastLoginMethod.mock.ts b/frontend/src/scenes/authentication/shared/lastLoginMethod.mock.tsnew file mode 100644index 000000000000..ab3f88d075f0--- /dev/null+++ b/frontend/src/scenes/authentication/shared/lastLoginMethod.mock.ts@@ -0,0 +1,11 @@+import { LoginMethod } from '~/types'++import { LAST_LOGIN_METHOD_COOKIE } from './lastLoginMethod'++export function setLastLoginMethodCookie(method: LoginMethod): void {+ if (method === null) {+ document.cookie = `${LAST_LOGIN_METHOD_COOKIE}=; max-age=0; path=/`+ return+ }+ document.cookie = `${LAST_LOGIN_METHOD_COOKIE}=${method}; path=/`+}diff --git a/frontend/src/scenes/authentication/shared/lastLoginMethod.ts b/frontend/src/scenes/authentication/shared/lastLoginMethod.tsnew file mode 100644index 000000000000..d64b3ddfe120--- /dev/null+++ b/frontend/src/scenes/authentication/shared/lastLoginMethod.ts@@ -0,0 +1,18 @@+import { useState } from 'react'++import { getCookie } from 'lib/api'++import { LoginMethod } from '~/types'++// pinned: posthog/middleware.py sets this cookie on a successful login, and the website reads it+export const LAST_LOGIN_METHOD_COOKIE = 'ph_last_login_method'++function readLastLoginMethod(): LoginMethod {+ return (getCookie(LAST_LOGIN_METHOD_COOKIE) as LoginMethod) ?? null+}++/** Read once per mount, because the cookie won't change while the page is open. */+export function useLastLoginMethod(): LoginMethod {+ const [lastLoginMethod] = useState(readLastLoginMethod)+ return lastLoginMethod+}diff --git a/frontend/src/scenes/authentication/signup/Signup.tsx b/frontend/src/scenes/authentication/signup/Signup.tsxindex 6b3d188eae79..ddadc7799236 100644--- a/frontend/src/scenes/authentication/signup/Signup.tsx+++ b/frontend/src/scenes/authentication/signup/Signup.tsx@@ -2,7 +2,6 @@ import { useActions, useMountedLogic, useValues } from 'kea'import { Form } from 'kea-forms'import { useState } from 'react'-import { getCookie } from 'lib/api'import PasswordStrength from 'lib/components/PasswordStrength'import SignupReferralSource from 'lib/components/SignupReferralSource'import SignupRoleSelect from 'lib/components/SignupRoleSelect'@@ -16,14 +15,13 @@ import { Link } from 'lib/lemon-ui/Link'import { AuthCardTitle } from 'scenes/authentication/shared/authScene/AuthCardTitle'import { AuthScene, AuthSceneCard } from 'scenes/authentication/shared/authScene/AuthScene'import { RegionField } from 'scenes/authentication/shared/authScene/RegionField'+import { useLastLoginMethod } from 'scenes/authentication/shared/lastLoginMethod'import { pendingOAuthConnectionLogic, reviewAccessCopy } from 'scenes/authentication/shared/pendingOAuthConnectionLogic'import { TurnstileChallenge } from 'scenes/authentication/signup/signupForm/TurnstileChallenge'import { preflightLogic } from 'scenes/PreflightCheck/preflightLogic'import { SceneExport } from 'scenes/sceneTypes'import { userLogic } from 'scenes/userLogic'-import { LoginMethod } from '~/types'-import { signupLogic } from './signupForm/signupLogic'export const scene: SceneExport = {@@ -46,7 +44,7 @@ function SignupEmailPanel(): JSX.Element {const { preflight } = useValues(preflightLogic)const { pendingConnection } = useValues(pendingOAuthConnectionLogic)const [showJoinOrg, setShowJoinOrg] = useState(false)- const lastLoginMethod = getCookie('ph_last_login_method') as LoginMethod | null+ const lastLoginMethod = useLastLoginMethod()const accountExists = !!signupPanelEmailManualErrors?.emailif (pendingInvite) {