posthog
This commit is contained in:
1 parent
e218ad57e8
commit
01a3c1a776
18 files changed
+1562
-53
No files matched your search
@@ -3,6 +3,7 @@ import { createContext, useContext, useState, useEffect } from 'react';
|
||||
import type { ReactNode } from 'react';
|
||||
import * as api from '../lib/api';
|
||||
import type { User } from '@/types';
|
||||
import { usePostHog } from '@posthog/react';
|
||||
|
||||
interface AuthContextType {
|
||||
user: User | null;
|
||||
@@ -25,6 +26,7 @@ export function AuthProvider({ children }: AuthProviderProps) {
|
||||
const [user, setUser] = useState<User | null>(null);
|
||||
const [token, setToken] = useState<string | null>(null);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const posthog = usePostHog();
|
||||
|
||||
useEffect(() => {
|
||||
const initializeAuth = () => {
|
||||
@@ -56,6 +58,15 @@ export function AuthProvider({ children }: AuthProviderProps) {
|
||||
setToken(response.access_token);
|
||||
setUser(response.user);
|
||||
setIsLoading(false);
|
||||
posthog.identify(response.user.id, {
|
||||
username: response.user.username,
|
||||
email: response.user.email,
|
||||
role: response.user.role,
|
||||
});
|
||||
posthog.capture('user_logged_in', {
|
||||
username: response.user.username,
|
||||
role: response.user.role,
|
||||
});
|
||||
};
|
||||
|
||||
const register = async (username: string, email: string, password: string) => {
|
||||
@@ -66,9 +77,20 @@ export function AuthProvider({ children }: AuthProviderProps) {
|
||||
setToken(response.access_token);
|
||||
setUser(response.user);
|
||||
setIsLoading(false);
|
||||
posthog.identify(response.user.id, {
|
||||
username: response.user.username,
|
||||
email: response.user.email,
|
||||
role: response.user.role,
|
||||
});
|
||||
posthog.capture('user_registered', {
|
||||
username: response.user.username,
|
||||
email: response.user.email,
|
||||
});
|
||||
};
|
||||
|
||||
const logout = () => {
|
||||
posthog.capture('user_logged_out');
|
||||
posthog.reset();
|
||||
localStorage.removeItem('token');
|
||||
localStorage.removeItem('user');
|
||||
setToken(null);
|
||||
|
||||
Reference in new issue
Block a user