: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 { MakeLogicType, actions, kea, listeners, path, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'
import { urlToAction } from 'kea-router'
import api from 'lib/api'
import { loginLogic } from 'scenes/authentication/auth/loginLogic'
import { urls } from 'scenes/urls'
export interface ValidatedTokenResponse {
success: boolean
token?: string
error?: string
requires_login?: boolean
}
export interface ResetResponse {
success: boolean
error?: string
requires_login?: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface twoFactorResetLogicValues {
currentToken: string
currentUuid: string
loginRedirectUrl: string
requiresLogin: boolean
resetComplete: boolean
resetError: string | null
resetLoading: boolean
resetResult: ResetResponse | null
resetResultLoading: boolean
validatedResetToken: ValidatedTokenResponse | null
validatedResetTokenLoading: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface twoFactorResetLogicActions {
confirmReset: (token: string) => {
token: string
}
executeReset: ({ uuid, token }: { token: string; uuid: string }) => {
uuid: string
token: string
}
executeResetFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
executeResetSuccess: (
resetResult: ResetResponse,
payload?: {
uuid: string
token: string
}
) => {
resetResult: ResetResponse
payload?: {
uuid: string
token: string
}
}
resetState: () => {
value: true
}
setRequiresLogin: (requires: boolean) => {
requires: boolean
}
setResetComplete: (complete: boolean) => {
complete: boolean
}
setResetError: (error: string | null) => {
error: string | null
}
validateResetToken: ({ uuid, token }: { token: string; uuid: string }) => {
uuid: string
token: string
}
validateResetTokenFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
validateResetTokenSuccess: (
validatedResetToken:
| ValidatedTokenResponse
| {
error: any
requires_login: boolean
success: false
},
payload?: {
uuid: string
token: string
}
) => {
validatedResetToken:
| ValidatedTokenResponse
| {
error: any
requires_login: boolean
success: false
}
payload?: {
uuid: string
token: string
}
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface twoFactorResetLogicMeta {
__keaTypeGenInternalSelectorTypes: {
loginRedirectUrl: (currentUuid: string, currentToken: string) => string
}
}
export type twoFactorResetLogicType = MakeLogicType<
twoFactorResetLogicValues,
twoFactorResetLogicActions,
Record<string, any>,
twoFactorResetLogicMeta
>
export const twoFactorResetLogic = kea<twoFactorResetLogicType>([
path(['scenes', 'authentication', 'two-factor-reset', 'twoFactorResetLogic']),
actions({
confirmReset: (token: string) => ({ token }),
setResetComplete: (complete: boolean) => ({ complete }),
setResetError: (error: string | null) => ({ error }),
setRequiresLogin: (requires: boolean) => ({ requires }),
resetState: true,
}),
loaders(({ actions }) => ({
validatedResetToken: [
null as ValidatedTokenResponse | null,
{
validateResetToken: async ({ uuid, token }: { uuid: string; token: string }) => {
try {
// nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. No generated function covers this endpoint yet. Find out why the generated client skips it (no schema, no product tag, or excluded from the spec) and fix that first.
const response = await api.get<ValidatedTokenResponse>(`api/reset_2fa/${uuid}/?token=${token}`)
return response
} catch (e: any) {
const requiresLogin = e.data?.requires_login === true
if (requiresLogin) {
actions.setRequiresLogin(true)
}
return {
success: false,
error: e.data?.error || e.detail || 'Invalid or expired link',
requires_login: requiresLogin,
}
}
},
},
],
resetResult: [
null as ResetResponse | null,
{
executeReset: async ({ uuid, token }: { uuid: string; token: string }) => {
// nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. No generated function covers this endpoint yet. Find out why the generated client skips it (no schema, no product tag, or excluded from the spec) and fix that first.
const response = await api.create<ResetResponse>(`api/reset_2fa/${uuid}/`, { token })
return response
},
},
],
})),
reducers({
validatedResetToken: {
resetState: () => null,
},
resetComplete: [
false,
{
setResetComplete: (_, { complete }) => complete,
resetState: () => false,
},
],
resetError: [
null as string | null,
{
setResetError: (_, { error }) => error,
executeReset: () => null,
resetState: () => null,
},
],
resetLoading: [
false,
{
executeReset: () => true,
executeResetSuccess: () => false,
executeResetFailure: () => false,
},
],
currentUuid: [
'' as string,
{
validateResetToken: (_, { uuid }) => uuid,
},
],
currentToken: [
'' as string,
{
validateResetToken: (_, { token }) => token,
},
],
requiresLogin: [
false as boolean,
{
setRequiresLogin: (_, { requires }) => requires,
validateResetTokenSuccess: (state, { validatedResetToken }) =>
validatedResetToken?.requires_login || state,
resetState: () => false,
},
],
}),
selectors({
loginRedirectUrl: [
(s) => [s.currentUuid, s.currentToken],
(uuid: string, token: string): string => {
// Redirect to login with the 2FA reset URL as the next parameter
const resetUrl = urls.twoFactorReset(uuid, token)
return `${urls.login()}?next=${encodeURIComponent(resetUrl)}`
},
],
}),
listeners(({ actions, values }) => ({
confirmReset: async ({ token }) => {
try {
actions.executeReset({ uuid: values.currentUuid, token })
} catch (e: any) {
actions.setResetError(e.data?.error || e.detail || 'Failed to reset 2FA. Please try again.')
}
},
executeResetSuccess: ({ resetResult }) => {
if (resetResult?.success) {
actions.setResetComplete(true)
// Reset the login form state so user starts fresh when redirected to login
loginLogic.actions.resetLogin()
} else if (resetResult?.requires_login) {
actions.setRequiresLogin(true)
} else {
actions.setResetError(resetResult?.error || 'Failed to reset 2FA. Please try again.')
}
},
executeResetFailure: ({ error }) => {
actions.setResetError(error || 'Failed to reset 2FA. Please try again.')
},
})),
urlToAction(({ actions }) => ({
'/reset_2fa/:uuid/:token': ({ uuid, token }) => {
if (token && uuid) {
actions.validateResetToken({ uuid, token })
}
},
})),
])