: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
refactor(auth): extract dev login logic to separate module (#64106)
Fernando Gomescf5eab0a2792
diff --git a/frontend/src/scenes/authentication/login/loginLogic.ts b/frontend/src/scenes/authentication/login/loginLogic.tsindex 23c131bdb3b1..35a0f9853db4 100644--- a/frontend/src/scenes/authentication/login/loginLogic.ts+++ b/frontend/src/scenes/authentication/login/loginLogic.ts@@ -9,6 +9,7 @@ import api from 'lib/api'import { lemonToast } from 'lib/lemon-ui/LemonToast'import { featureFlagLogic } from 'lib/logic/featureFlagLogic'import { getRelativeNextPath } from 'lib/utils'+import { devLoginLogic } from 'scenes/authentication/shared/devLoginLogic'import { twoFactorResetLogic } from 'scenes/authentication/two-factor-reset/twoFactorResetLogic'import { preflightLogic } from 'scenes/PreflightCheck/preflightLogic'import { urls } from 'scenes/urls'@@ -30,14 +31,6 @@ export interface PrecheckResponseType {webauthn_credentials?: PublicKeyCredentialDescriptorJSON[]}-export interface DevUser {- email: string- is_staff: boolean- label: string | null-}--export const DEV_LOGIN_SECONDS_SAVED_PER_CLICK = 5-// Routes that should be handled by Django, not the React routerconst BACKEND_ONLY_ROUTES = ['/login/vercel/continue',@@ -83,12 +76,19 @@ export interface TwoFactorForm {export const loginLogic = kea<loginLogicType>([path(['scenes', 'authentication', 'login', 'loginLogic']),connect(() => ({- values: [preflightLogic, ['preflight'], featureFlagLogic, ['featureFlags']],+ values: [+ preflightLogic,+ ['preflight'],+ featureFlagLogic,+ ['featureFlags'],+ devLoginLogic,+ ['devUsers', 'devUsersLoading', 'devLoginTimeSavedLabel'],+ ],+ actions: [devLoginLogic, ['devLogin', 'loadDevUsers']],})),actions({setGeneralError: (code: string, detail: string) => ({ code, detail }),clearGeneralError: true,- devLogin: (email: string) => ({ email }),}),reducers({// This is separate from the login form, so that the form can be submitted even if a general error is present@@ -99,13 +99,6 @@ export const loginLogic = kea<loginLogicType>([clearGeneralError: () => null,},],- devLoginCount: [- 0,- { persist: true },- {- devLogin: (count) => count + 1,- },- ],}),loaders(() => ({precheckResponse: [@@ -129,22 +122,6 @@ export const loginLogic = kea<loginLogicType>([},},],- devUsers: [- [] as DevUser[],- {- // Not fired on an `onMount` because we don't always need it.- loadDevUsers: async (_, breakpoint) => {- breakpoint()- try {- const response = await api.get<{ users: DevUser[] }>('api/login/dev')- return response.users- } catch {- // Endpoint is unavailable unless allow_dev_login is set in preflight.- return []- }- },- },- ],resendResponse: [null as { success: boolean; message: string } | null,{@@ -175,24 +152,6 @@ export const loginLogic = kea<loginLogicType>([return nextParam ? `/signup?next=${encodeURIComponent(nextParam)}` : '/signup'},],- devLoginTimeSavedLabel: [- (s) => [s.devLoginCount],- (devLoginCount): string | null => {- if (devLoginCount === 0) {- return null- }-- const totalSeconds = devLoginCount * DEV_LOGIN_SECONDS_SAVED_PER_CLICK- if (totalSeconds < 60) {- const unit = totalSeconds === 1 ? 'second' : 'seconds'- return `You've saved ${totalSeconds} ${unit} by clicking this button.`- }-- const minutes = Math.floor(totalSeconds / 60)- const unit = minutes === 1 ? 'minute' : 'minutes'- return `You've saved ${minutes} ${unit} by clicking this button.`- },- ],})),forms(({ actions }) => ({login: {@@ -236,24 +195,12 @@ export const loginLogic = kea<loginLogicType>([},},})),- listeners(({ actions, values }) => ({+ listeners(({ values }) => ({submitLoginSuccess: () => {handleLoginRedirect()// Reload the page after login to ensure POSTHOG_APP_CONTEXT is set correctly.window.location.reload()},- devLogin: async ({ email }) => {- actions.clearGeneralError()- try {- await api.create<any>('api/login/dev', { email })- } catch (e) {- const { code, detail } = e as Record<string, any>- actions.setGeneralError(code || 'dev_login_failed', detail || 'Dev login failed')- return- }- handleLoginRedirect()- window.location.reload()- },precheckSuccess: async (_, breakpoint) => {const { precheckResponse } = values// Auto-trigger passkey prompt if user has passkeys and SSO is not enforceddiff --git a/frontend/src/scenes/authentication/shared/devLoginLogic.ts b/frontend/src/scenes/authentication/shared/devLoginLogic.tsnew file mode 100644index 000000000000..bff039cf5c4e--- /dev/null+++ b/frontend/src/scenes/authentication/shared/devLoginLogic.ts@@ -0,0 +1,84 @@+import { actions, kea, listeners, path, reducers, selectors } from 'kea'+import { loaders } from 'kea-loaders'++import api from 'lib/api'++import type { devLoginLogicType } from './devLoginLogicType'++export interface DevUser {+ email: string+ is_staff: boolean+ label: string | null+}++export const DEV_LOGIN_SECONDS_SAVED_PER_CLICK = 5++export const devLoginLogic = kea<devLoginLogicType>([+ path(['scenes', 'authentication', 'shared', 'devLoginLogic']),+ actions({+ devLogin: (email: string) => ({ email }),+ }),+ reducers({+ devLoginCount: [+ 0,+ { persist: true },+ {+ devLogin: (count) => count + 1,+ },+ ],+ }),+ loaders(() => ({+ devUsers: [+ [] as DevUser[],+ {+ // Not fired on an `onMount` because we don't always need it.+ loadDevUsers: async (_, breakpoint) => {+ breakpoint()+ try {+ const response = await api.get<{ users: DevUser[] }>('api/login/dev')+ return response.users+ } catch {+ // Endpoint is unavailable unless allow_dev_login is set in preflight.+ return []+ }+ },+ },+ ],+ })),+ selectors(() => ({+ devLoginTimeSavedLabel: [+ (s) => [s.devLoginCount],+ (devLoginCount): string | null => {+ if (devLoginCount === 0) {+ return null+ }++ const totalSeconds = devLoginCount * DEV_LOGIN_SECONDS_SAVED_PER_CLICK+ if (totalSeconds < 60) {+ const unit = totalSeconds === 1 ? 'second' : 'seconds'+ return `You've saved ${totalSeconds} ${unit} by clicking this button.`+ }++ const minutes = Math.floor(totalSeconds / 60)+ const unit = minutes === 1 ? 'minute' : 'minutes'+ return `You've saved ${minutes} ${unit} by clicking this button.`+ },+ ],+ })),+ listeners(() => ({+ devLogin: async ({ email }) => {+ // Dynamic import to avoid a circular dependency: loginLogic statically imports this logic.+ const { loginLogic, handleLoginRedirect } = await import('scenes/authentication/login/loginLogic')+ loginLogic.actions.clearGeneralError()+ try {+ await api.create<any>('api/login/dev', { email })+ } catch (e) {+ const { code, detail } = e as Record<string, any>+ loginLogic.actions.setGeneralError(code || 'dev_login_failed', detail || 'Dev login failed')+ return+ }+ handleLoginRedirect()+ window.location.reload()+ },+ })),+])