Skip to content

: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.

3.3 KB
Raw Blame
import { MakeLogicType, actions, events, kea, path } from 'kea'
import { loaders } from 'kea-loaders'

import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'

import {
    usersLoginSessionsDestroy,
    usersLoginSessionsList,
    usersLoginSessionsRevokeOthersCreate,
} from '~/generated/core/api'
import type { UserAuthSessionApi } from '~/generated/core/api.schemas'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface loginSessionsLogicValues {
    loginSessions: UserAuthSessionApi[]
    loginSessionsLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface loginSessionsLogicActions {
    loadLoginSessions: () => any
    loadLoginSessionsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadLoginSessionsSuccess: (
        loginSessions: UserAuthSessionApi[],
        payload?: any
    ) => {
        loginSessions: UserAuthSessionApi[]
        payload?: any
    }
    revokeOtherSessions: () => {
        value: true
    }
    revokeOtherSessionsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    revokeOtherSessionsSuccess: (
        loginSessions: UserAuthSessionApi[],
        payload?: {
            value: true
        }
    ) => {
        loginSessions: UserAuthSessionApi[]
        payload?: {
            value: true
        }
    }
    revokeSession: (id: string) => {
        id: string
    }
    revokeSessionFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    revokeSessionSuccess: (
        loginSessions: UserAuthSessionApi[],
        payload?: {
            id: string
        }
    ) => {
        loginSessions: UserAuthSessionApi[]
        payload?: {
            id: string
        }
    }
}

export type loginSessionsLogicType = MakeLogicType<loginSessionsLogicValues, loginSessionsLogicActions>

export const loginSessionsLogic = kea<loginSessionsLogicType>([
    path(['scenes', 'settings', 'user', 'loginSessionsLogic']),

    actions({
        revokeSession: (id: string) => ({ id }),
        revokeOtherSessions: true,
    }),

    loaders(({ values }) => ({
        loginSessions: [
            [] as UserAuthSessionApi[],
            {
                loadLoginSessions: async () => {
                    return await usersLoginSessionsList('@me')
                },
                revokeSession: async ({ id }) => {
                    await usersLoginSessionsDestroy('@me', id)
                    lemonToast.success('Logged out of that device')
                    return values.loginSessions.filter((session) => session.id !== id)
                },
                revokeOtherSessions: async () => {
                    const { revoked_count } = await usersLoginSessionsRevokeOthersCreate('@me')
                    lemonToast.success(
                        `Logged out of ${revoked_count} other ${revoked_count === 1 ? 'device' : 'devices'}`
                    )
                    return values.loginSessions.filter((session) => session.is_current)
                },
            },
        ],
    })),

    events(({ actions }) => ({
        afterMount: () => actions.loadLoginSessions(),
    })),
])