Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/components/PostHogPageView.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useEffect } from 'react';
import { useLocation } from 'react-router-dom';
import { usePostHog } from 'posthog-js/react';
import { api, isAuthenticated } from '../lib/api';

/**
* Listens to React Router route changes and fires a PostHog $pageview event
Expand All @@ -17,5 +18,29 @@ export default function PostHogPageView() {
});
}, [location, posthog]);

// Identify user on load and on auth-change
useEffect(() => {
const identifyUser = async () => {
if (isAuthenticated()) {
try {
const profile = await api.getMe();
posthog?.identify(profile.id, {
email: profile.email,
name: profile.full_name,
});
} catch (err) {
console.error("Failed to identify user with backend", err);
}
}
};

// Run on initial load if returning user
identifyUser();

// Run whenever the token is set or cleared
window.addEventListener('auth-change', identifyUser);
return () => window.removeEventListener('auth-change', identifyUser);
}, [posthog]);

return null;
}
8 changes: 0 additions & 8 deletions src/pages/AuthPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,6 @@ export default function AuthPage() {
Promise.resolve().then(() => setStatus('processing'));
setToken(accessToken);
window.history.replaceState({}, '', '/auth');
// Identify the user in PostHog using their JWT sub claim as the ID.
// The token is a JWT; we decode the payload to get the user ID.
try {
const payload = JSON.parse(atob(accessToken.split('.')[1]));
posthog?.identify(payload.sub, { email: payload.email });
} catch {
// Not a JWT or malformed — skip identification silently
}
navigate('/mode', { replace: true });
return;
}
Expand Down
Loading