(frontend) login and logout from Posthog

Following Posthog documentation, login and logout users directly
from the client. Note: Logout function should be encapsulate in
a proper function.
This commit is contained in:
lebaudantoine
2024-09-17 12:47:33 +02:00
committed by aleb_the_flash
parent 11664956c7
commit 178278235e
2 changed files with 10 additions and 0 deletions

View File

@@ -2,6 +2,8 @@ import { useQuery } from '@tanstack/react-query'
import { keys } from '@/api/queryKeys'
import { fetchUser } from './fetchUser'
import { type ApiUser } from './ApiUser'
import { useEffect } from 'react'
import posthog from 'posthog-js'
/**
* returns info about currently logged in user
@@ -15,6 +17,12 @@ export const useUser = () => {
staleTime: 1000 * 60 * 60, // 1 hour
})
useEffect(() => {
if (query.data && query.data.id && !posthog._isIdentified()) {
posthog.identify('query.data.id', { email: query.data.email })
}
}, [query.data])
const isLoggedIn =
query.status === 'success' ? query.data !== false : undefined
const isLoggedOut = isLoggedIn === false

View File

@@ -9,6 +9,7 @@ import { useMatchesRoute } from '@/navigation/useMatchesRoute'
import { Feedback } from '@/components/Feedback'
import { Menu } from '@/primitives/Menu'
import { MenuList } from '@/primitives/MenuList'
import posthog from 'posthog-js'
export const Header = () => {
const { t } = useTranslation()
@@ -79,6 +80,7 @@ export const Header = () => {
items={[{ value: 'logout', label: t('logout') }]}
onAction={(value) => {
if (value === 'logout') {
posthog.reset()
window.location.href = logoutUrl()
}
}}